OLD | NEW |
(Empty) | |
| 1 //===- ReplacePtrsWithInts.cpp - Convert pointer values to integer values--===// |
| 2 // |
| 3 // The LLVM Compiler Infrastructure |
| 4 // |
| 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. |
| 7 // |
| 8 //===----------------------------------------------------------------------===// |
| 9 // |
| 10 // This pass strips out aggregate pointer types and replaces them with |
| 11 // the integer type iPTR, which is i32 for PNaCl (though this pass |
| 12 // will allow iPTR to be i64 if the DataLayout specifies 64-bit |
| 13 // pointers). |
| 14 // |
| 15 // This pass relies on -simplify-allocas to transform allocas into arrays of |
| 16 // bytes. |
| 17 // |
| 18 // The pass converts IR to the following normal form: |
| 19 // |
| 20 // All inttoptr and ptrtoint instructions use the same integer size |
| 21 // (iPTR), so they do not implicitly truncate or zero-extend. |
| 22 // |
| 23 // Pointer types only appear in the following instructions: |
| 24 // * loads and stores: the pointer operand is a NormalizedPtr. |
| 25 // * function calls: the function operand is a NormalizedPtr. |
| 26 // * intrinsic calls: any pointer arguments are NormalizedPtrs. |
| 27 // * alloca |
| 28 // * bitcast and inttoptr: only used as part of a NormalizedPtr. |
| 29 // * ptrtoint: the operand is an InherentPtr. |
| 30 // |
| 31 // Where an InherentPtr is defined as a pointer value that is: |
| 32 // * an alloca; |
| 33 // * a GlobalValue (a function or global variable); or |
| 34 // * an intrinsic call. |
| 35 // |
| 36 // And a NormalizedPtr is defined as a pointer value that is: |
| 37 // * an inttoptr instruction; |
| 38 // * an InherentPtr; or |
| 39 // * a bitcast of an InherentPtr. |
| 40 // |
| 41 // This pass currently strips out lifetime markers (that is, calls to |
| 42 // the llvm.lifetime.start/end intrinsics) and invariant markers |
| 43 // (calls to llvm.invariant.start/end). |
| 44 // |
| 45 //===----------------------------------------------------------------------===// |
| 46 |
| 47 #include "llvm/ADT/DenseMap.h" |
| 48 #include "llvm/ADT/SmallPtrSet.h" |
| 49 #include "llvm/IR/DataLayout.h" |
| 50 #include "llvm/IR/DebugInfo.h" |
| 51 #include "llvm/IR/DerivedTypes.h" |
| 52 #include "llvm/IR/Function.h" |
| 53 #include "llvm/IR/Instructions.h" |
| 54 #include "llvm/IR/IntrinsicInst.h" |
| 55 #include "llvm/IR/Module.h" |
| 56 #include "llvm/IR/Type.h" |
| 57 #include "llvm/Pass.h" |
| 58 #include "llvm/Support/raw_ostream.h" |
| 59 #include "llvm/Transforms/NaCl.h" |
| 60 |
| 61 using namespace llvm; |
| 62 |
| 63 namespace { |
| 64 // This is a ModulePass because the pass must recreate functions in |
| 65 // order to change their argument and return types. |
| 66 struct ReplacePtrsWithInts : public ModulePass { |
| 67 static char ID; // Pass identification, replacement for typeid |
| 68 ReplacePtrsWithInts() : ModulePass(ID) { |
| 69 initializeReplacePtrsWithIntsPass(*PassRegistry::getPassRegistry()); |
| 70 } |
| 71 |
| 72 virtual bool runOnModule(Module &M); |
| 73 }; |
| 74 |
| 75 // FunctionConverter stores the state for mapping old instructions |
| 76 // (of pointer type) to converted instructions (of integer type) |
| 77 // within a function, and provides methods for doing the conversion. |
| 78 class FunctionConverter { |
| 79 // Int type that pointer types are to be replaced with, typically i32. |
| 80 Type *IntPtrType; |
| 81 |
| 82 struct RewrittenVal { |
| 83 RewrittenVal(): Placeholder(NULL), NewIntVal(NULL) {} |
| 84 Value *Placeholder; |
| 85 Value *NewIntVal; |
| 86 }; |
| 87 // Maps from old values (of pointer type) to converted values (of |
| 88 // IntPtrType type). |
| 89 DenseMap<Value *, RewrittenVal> RewriteMap; |
| 90 |
| 91 public: |
| 92 FunctionConverter(Type *IntPtrType) : IntPtrType(IntPtrType) {} |
| 93 |
| 94 // Returns the normalized version of the given type, converting |
| 95 // pointer types to IntPtrType. |
| 96 Type *convertType(Type *Ty); |
| 97 // Returns the normalized version of the given function type by |
| 98 // normalizing the function's argument types. |
| 99 FunctionType *convertFuncType(FunctionType *FTy); |
| 100 |
| 101 // Records that 'To' is the normalized version of 'From'. If 'To' |
| 102 // is not of pointer type, no type conversion is required, so this |
| 103 // can take the short cut of replacing 'To' with 'From'. |
| 104 void recordConverted(Value *From, Value *To); |
| 105 void recordConvertedAndErase(Instruction *From, Value *To); |
| 106 |
| 107 // Returns Val with no-op casts (those that convert between |
| 108 // IntPtrType and pointer types) stripped off. |
| 109 Value *stripNoopCasts(Value *Val); |
| 110 |
| 111 // Returns the normalized version of the given value. |
| 112 // |
| 113 // If the conversion of Val has been deferred, this returns a |
| 114 // placeholder object, which will later be replaceAllUsesWith'd to |
| 115 // the final value. Since replaceAllUsesWith does not work on |
| 116 // references by metadata nodes, this can be bypassed using |
| 117 // BypassPlaceholder to get the real converted value, assuming it |
| 118 // is available. |
| 119 Value *convert(Value *Val, bool BypassPlaceholder = false); |
| 120 // Returns the NormalizedPtr form of the given pointer value. |
| 121 // Inserts conversion instructions at InsertPt. |
| 122 Value *convertBackToPtr(Value *Val, Instruction *InsertPt); |
| 123 // Returns the NormalizedPtr form of the given function pointer. |
| 124 // Inserts conversion instructions at InsertPt. |
| 125 Value *convertFunctionPtr(Value *Callee, Instruction *InsertPt); |
| 126 // Converts an instruction without recreating it, by wrapping its |
| 127 // operands and result. |
| 128 void convertInPlace(Instruction *Inst); |
| 129 |
| 130 void eraseReplacedInstructions(); |
| 131 |
| 132 // List of instructions whose deletion has been deferred. |
| 133 SmallVector<Instruction *, 20> ToErase; |
| 134 }; |
| 135 } |
| 136 |
| 137 Type *FunctionConverter::convertType(Type *Ty) { |
| 138 if (Ty->isPointerTy()) |
| 139 return IntPtrType; |
| 140 return Ty; |
| 141 } |
| 142 |
| 143 FunctionType *FunctionConverter::convertFuncType(FunctionType *FTy) { |
| 144 SmallVector<Type *, 8> ArgTypes; |
| 145 for (FunctionType::param_iterator ArgTy = FTy->param_begin(), |
| 146 E = FTy->param_end(); ArgTy != E; ++ArgTy) { |
| 147 ArgTypes.push_back(convertType(*ArgTy)); |
| 148 } |
| 149 return FunctionType::get(convertType(FTy->getReturnType()), ArgTypes, |
| 150 FTy->isVarArg()); |
| 151 } |
| 152 |
| 153 void FunctionConverter::recordConverted(Value *From, Value *To) { |
| 154 if (!From->getType()->isPointerTy()) { |
| 155 From->replaceAllUsesWith(To); |
| 156 return; |
| 157 } |
| 158 RewrittenVal *RV = &RewriteMap[From]; |
| 159 assert(!RV->NewIntVal); |
| 160 RV->NewIntVal = To; |
| 161 } |
| 162 |
| 163 void FunctionConverter::recordConvertedAndErase(Instruction *From, Value *To) { |
| 164 recordConverted(From, To); |
| 165 // There may still be references to this value, so defer deleting it. |
| 166 ToErase.push_back(From); |
| 167 } |
| 168 |
| 169 Value *FunctionConverter::stripNoopCasts(Value *Val) { |
| 170 SmallPtrSet<Value *, 4> Visited; |
| 171 for (;;) { |
| 172 if (!Visited.insert(Val).second) { |
| 173 // It is possible to get a circular reference in unreachable |
| 174 // basic blocks. Handle this case for completeness. |
| 175 return UndefValue::get(Val->getType()); |
| 176 } |
| 177 if (CastInst *Cast = dyn_cast<CastInst>(Val)) { |
| 178 Value *Src = Cast->getOperand(0); |
| 179 if ((isa<BitCastInst>(Cast) && Cast->getType()->isPointerTy()) || |
| 180 (isa<PtrToIntInst>(Cast) && Cast->getType() == IntPtrType) || |
| 181 (isa<IntToPtrInst>(Cast) && Src->getType() == IntPtrType)) { |
| 182 Val = Src; |
| 183 continue; |
| 184 } |
| 185 } |
| 186 return Val; |
| 187 } |
| 188 } |
| 189 |
| 190 Value *FunctionConverter::convert(Value *Val, bool BypassPlaceholder) { |
| 191 Val = stripNoopCasts(Val); |
| 192 if (!Val->getType()->isPointerTy()) |
| 193 return Val; |
| 194 if (Constant *C = dyn_cast<Constant>(Val)) |
| 195 return ConstantExpr::getPtrToInt(C, IntPtrType); |
| 196 RewrittenVal *RV = &RewriteMap[Val]; |
| 197 if (BypassPlaceholder) { |
| 198 assert(RV->NewIntVal); |
| 199 return RV->NewIntVal; |
| 200 } |
| 201 if (!RV->Placeholder) |
| 202 RV->Placeholder = new Argument(convertType(Val->getType())); |
| 203 return RV->Placeholder; |
| 204 } |
| 205 |
| 206 Value *FunctionConverter::convertBackToPtr(Value *Val, Instruction *InsertPt) { |
| 207 Type *NewTy = |
| 208 convertType(Val->getType()->getPointerElementType())->getPointerTo(); |
| 209 return new IntToPtrInst(convert(Val), NewTy, "", InsertPt); |
| 210 } |
| 211 |
| 212 Value *FunctionConverter::convertFunctionPtr(Value *Callee, |
| 213 Instruction *InsertPt) { |
| 214 FunctionType *FuncType = cast<FunctionType>( |
| 215 Callee->getType()->getPointerElementType()); |
| 216 return new IntToPtrInst(convert(Callee), |
| 217 convertFuncType(FuncType)->getPointerTo(), |
| 218 "", InsertPt); |
| 219 } |
| 220 |
| 221 static bool ShouldLeaveAlone(Value *V) { |
| 222 if (Function *F = dyn_cast<Function>(V)) |
| 223 return F->isIntrinsic(); |
| 224 if (isa<InlineAsm>(V)) |
| 225 return true; |
| 226 return false; |
| 227 } |
| 228 |
| 229 void FunctionConverter::convertInPlace(Instruction *Inst) { |
| 230 // Convert operands. |
| 231 for (unsigned I = 0; I < Inst->getNumOperands(); ++I) { |
| 232 Value *Arg = Inst->getOperand(I); |
| 233 if (Arg->getType()->isPointerTy() && !ShouldLeaveAlone(Arg)) { |
| 234 Value *Conv = convert(Arg); |
| 235 Inst->setOperand(I, new IntToPtrInst(Conv, Arg->getType(), "", Inst)); |
| 236 } |
| 237 } |
| 238 // Convert result. |
| 239 if (Inst->getType()->isPointerTy()) { |
| 240 Instruction *Cast = new PtrToIntInst( |
| 241 Inst, convertType(Inst->getType()), Inst->getName() + ".asint"); |
| 242 Cast->insertAfter(Inst); |
| 243 recordConverted(Inst, Cast); |
| 244 } |
| 245 } |
| 246 |
| 247 void FunctionConverter::eraseReplacedInstructions() { |
| 248 bool Error = false; |
| 249 for (DenseMap<Value *, RewrittenVal>::iterator I = RewriteMap.begin(), |
| 250 E = RewriteMap.end(); I != E; ++I) { |
| 251 if (I->second.Placeholder) { |
| 252 if (I->second.NewIntVal) { |
| 253 I->second.Placeholder->replaceAllUsesWith(I->second.NewIntVal); |
| 254 } else { |
| 255 errs() << "Not converted: " << *I->first << "\n"; |
| 256 Error = true; |
| 257 } |
| 258 } |
| 259 } |
| 260 if (Error) |
| 261 report_fatal_error("Case not handled in ReplacePtrsWithInts"); |
| 262 |
| 263 // Delete the placeholders in a separate pass. This means that if |
| 264 // one placeholder is accidentally rewritten to another, we will get |
| 265 // a useful error message rather than accessing a dangling pointer. |
| 266 for (DenseMap<Value *, RewrittenVal>::iterator I = RewriteMap.begin(), |
| 267 E = RewriteMap.end(); I != E; ++I) { |
| 268 delete I->second.Placeholder; |
| 269 } |
| 270 |
| 271 // We must do dropAllReferences() before doing eraseFromParent(), |
| 272 // otherwise we will try to erase instructions that are still |
| 273 // referenced. |
| 274 for (SmallVectorImpl<Instruction *>::iterator I = ToErase.begin(), |
| 275 E = ToErase.end(); |
| 276 I != E; ++I) { |
| 277 (*I)->dropAllReferences(); |
| 278 } |
| 279 for (SmallVectorImpl<Instruction *>::iterator I = ToErase.begin(), |
| 280 E = ToErase.end(); |
| 281 I != E; ++I) { |
| 282 (*I)->eraseFromParent(); |
| 283 } |
| 284 } |
| 285 |
| 286 static void ConvertMetadataOperand(FunctionConverter *FC, |
| 287 IntrinsicInst *Call, int Index) { |
| 288 MDNode *MD = cast<MDNode>(Call->getArgOperand(Index)); |
| 289 if (MD->getNumOperands() != 1) |
| 290 return; |
| 291 Value *MDArg = MD->getOperand(0); |
| 292 if (MDArg && (isa<Argument>(MDArg) || isa<Instruction>(MDArg))) { |
| 293 MDArg = FC->convert(MDArg, /* BypassPlaceholder= */ true); |
| 294 if (PtrToIntInst *Cast = dyn_cast<PtrToIntInst>(MDArg)) { |
| 295 // Unwrapping this is necessary for llvm.dbg.declare to work. |
| 296 MDArg = Cast->getPointerOperand(); |
| 297 } |
| 298 SmallVector<Value *, 1> Args; |
| 299 Args.push_back(MDArg); |
| 300 Call->setArgOperand(Index, MDNode::get(Call->getContext(), Args)); |
| 301 } |
| 302 } |
| 303 |
| 304 // Remove attributes that only apply to pointer arguments. Returns |
| 305 // the updated AttributeSet. |
| 306 static AttributeSet RemovePointerAttrs(LLVMContext &Context, |
| 307 AttributeSet Attrs) { |
| 308 SmallVector<AttributeSet, 8> AttrList; |
| 309 for (unsigned Slot = 0; Slot < Attrs.getNumSlots(); ++Slot) { |
| 310 unsigned Index = Attrs.getSlotIndex(Slot); |
| 311 AttrBuilder AB; |
| 312 for (AttributeSet::iterator Attr = Attrs.begin(Slot), E = Attrs.end(Slot); |
| 313 Attr != E; ++Attr) { |
| 314 if (!Attr->isEnumAttribute()) { |
| 315 continue; |
| 316 } |
| 317 switch (Attr->getKindAsEnum()) { |
| 318 // ByVal and StructRet should already have been removed by the |
| 319 // ExpandByVal pass. |
| 320 case Attribute::ByVal: |
| 321 case Attribute::StructRet: |
| 322 case Attribute::Nest: |
| 323 Attrs.dump(); |
| 324 report_fatal_error("ReplacePtrsWithInts cannot handle " |
| 325 "byval, sret or nest attrs"); |
| 326 break; |
| 327 // Strip these attributes because they apply only to pointers. This pass |
| 328 // rewrites pointer arguments, thus these parameter attributes are |
| 329 // meaningless. Also, they are rejected by the PNaCl module verifier. |
| 330 case Attribute::NoCapture: |
| 331 case Attribute::NoAlias: |
| 332 case Attribute::ReadNone: |
| 333 case Attribute::ReadOnly: |
| 334 break; |
| 335 default: |
| 336 AB.addAttribute(*Attr); |
| 337 } |
| 338 } |
| 339 AttrList.push_back(AttributeSet::get(Context, Index, AB)); |
| 340 } |
| 341 return AttributeSet::get(Context, AttrList); |
| 342 } |
| 343 |
| 344 static void ConvertInstruction(DataLayout *DL, Type *IntPtrType, |
| 345 FunctionConverter *FC, Instruction *Inst) { |
| 346 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Inst)) { |
| 347 Value *Result = Ret->getReturnValue(); |
| 348 if (Result) |
| 349 Result = FC->convert(Result); |
| 350 CopyDebug(ReturnInst::Create(Ret->getContext(), Result, Ret), Inst); |
| 351 Ret->eraseFromParent(); |
| 352 } else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) { |
| 353 PHINode *Phi2 = PHINode::Create(FC->convertType(Phi->getType()), |
| 354 Phi->getNumIncomingValues(), |
| 355 "", Phi); |
| 356 CopyDebug(Phi2, Phi); |
| 357 for (unsigned I = 0; I < Phi->getNumIncomingValues(); ++I) { |
| 358 Phi2->addIncoming(FC->convert(Phi->getIncomingValue(I)), |
| 359 Phi->getIncomingBlock(I)); |
| 360 } |
| 361 Phi2->takeName(Phi); |
| 362 FC->recordConvertedAndErase(Phi, Phi2); |
| 363 } else if (SelectInst *Op = dyn_cast<SelectInst>(Inst)) { |
| 364 Instruction *Op2 = SelectInst::Create(Op->getCondition(), |
| 365 FC->convert(Op->getTrueValue()), |
| 366 FC->convert(Op->getFalseValue()), |
| 367 "", Op); |
| 368 CopyDebug(Op2, Op); |
| 369 Op2->takeName(Op); |
| 370 FC->recordConvertedAndErase(Op, Op2); |
| 371 } else if (isa<PtrToIntInst>(Inst) || isa<IntToPtrInst>(Inst)) { |
| 372 Value *Arg = FC->convert(Inst->getOperand(0)); |
| 373 Type *ResultTy = FC->convertType(Inst->getType()); |
| 374 unsigned ArgSize = Arg->getType()->getIntegerBitWidth(); |
| 375 unsigned ResultSize = ResultTy->getIntegerBitWidth(); |
| 376 Value *Result; |
| 377 // We avoid using IRBuilder's CreateZExtOrTrunc() here because it |
| 378 // constant-folds ptrtoint ConstantExprs. This leads to creating |
| 379 // ptrtoints of non-IntPtrType type, which is not what we want, |
| 380 // because we want truncation/extension to be done explicitly by |
| 381 // separate instructions. |
| 382 if (ArgSize == ResultSize) { |
| 383 Result = Arg; |
| 384 } else { |
| 385 Instruction::CastOps CastType = |
| 386 ArgSize > ResultSize ? Instruction::Trunc : Instruction::ZExt; |
| 387 Result = CopyDebug(CastInst::Create(CastType, Arg, ResultTy, "", Inst), |
| 388 Inst); |
| 389 } |
| 390 if (Result != Arg) |
| 391 Result->takeName(Inst); |
| 392 FC->recordConvertedAndErase(Inst, Result); |
| 393 } else if (isa<BitCastInst>(Inst)) { |
| 394 if (Inst->getType()->isPointerTy()) { |
| 395 FC->ToErase.push_back(Inst); |
| 396 } |
| 397 } else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Inst)) { |
| 398 Value *Cmp2 = CopyDebug(new ICmpInst(Inst, Cmp->getPredicate(), |
| 399 FC->convert(Cmp->getOperand(0)), |
| 400 FC->convert(Cmp->getOperand(1)), ""), |
| 401 Inst); |
| 402 Cmp2->takeName(Cmp); |
| 403 Cmp->replaceAllUsesWith(Cmp2); |
| 404 Cmp->eraseFromParent(); |
| 405 } else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { |
| 406 Value *Ptr = FC->convertBackToPtr(Load->getPointerOperand(), Inst); |
| 407 LoadInst *Result = new LoadInst(Ptr, "", Inst); |
| 408 Result->takeName(Inst); |
| 409 CopyDebug(Result, Inst); |
| 410 CopyLoadOrStoreAttrs(Result, Load); |
| 411 FC->recordConvertedAndErase(Inst, Result); |
| 412 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { |
| 413 Value *Ptr = FC->convertBackToPtr(Store->getPointerOperand(), Inst); |
| 414 StoreInst *Result = new StoreInst(FC->convert(Store->getValueOperand()), |
| 415 Ptr, Inst); |
| 416 CopyDebug(Result, Inst); |
| 417 CopyLoadOrStoreAttrs(Result, Store); |
| 418 Inst->eraseFromParent(); |
| 419 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { |
| 420 if (IntrinsicInst *ICall = dyn_cast<IntrinsicInst>(Inst)) { |
| 421 if (ICall->getIntrinsicID() == Intrinsic::lifetime_start || |
| 422 ICall->getIntrinsicID() == Intrinsic::lifetime_end || |
| 423 ICall->getIntrinsicID() == Intrinsic::invariant_start) { |
| 424 // Remove alloca lifetime markers for now. This is because |
| 425 // the GVN pass can introduce lifetime markers taking PHI |
| 426 // nodes as arguments. If ReplacePtrsWithInts converts the |
| 427 // PHI node to int type, we will render those lifetime markers |
| 428 // ineffective. But dropping a subset of lifetime markers is |
| 429 // not safe in general. So, until LLVM better defines the |
| 430 // semantics of lifetime markers, we drop them all. See: |
| 431 // https://code.google.com/p/nativeclient/issues/detail?id=3443 |
| 432 // We do the same for invariant.start/end because they work in |
| 433 // a similar way. |
| 434 Inst->eraseFromParent(); |
| 435 } else { |
| 436 FC->convertInPlace(Inst); |
| 437 } |
| 438 } else if (isa<InlineAsm>(Call->getCalledValue())) { |
| 439 FC->convertInPlace(Inst); |
| 440 } else { |
| 441 SmallVector<Value *, 10> Args; |
| 442 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) |
| 443 Args.push_back(FC->convert(Call->getArgOperand(I))); |
| 444 CallInst *NewCall = CallInst::Create( |
| 445 FC->convertFunctionPtr(Call->getCalledValue(), Call), |
| 446 Args, "", Inst); |
| 447 CopyDebug(NewCall, Call); |
| 448 NewCall->setAttributes(RemovePointerAttrs(Call->getContext(), |
| 449 Call->getAttributes())); |
| 450 NewCall->setCallingConv(Call->getCallingConv()); |
| 451 NewCall->setTailCall(Call->isTailCall()); |
| 452 NewCall->takeName(Call); |
| 453 FC->recordConvertedAndErase(Call, NewCall); |
| 454 } |
| 455 } else if (InvokeInst *Call = dyn_cast<InvokeInst>(Inst)) { |
| 456 SmallVector<Value *, 10> Args; |
| 457 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) |
| 458 Args.push_back(FC->convert(Call->getArgOperand(I))); |
| 459 InvokeInst *NewCall = InvokeInst::Create( |
| 460 FC->convertFunctionPtr(Call->getCalledValue(), Call), |
| 461 Call->getNormalDest(), |
| 462 Call->getUnwindDest(), |
| 463 Args, "", Inst); |
| 464 CopyDebug(NewCall, Call); |
| 465 NewCall->setAttributes(RemovePointerAttrs(Call->getContext(), |
| 466 Call->getAttributes())); |
| 467 NewCall->setCallingConv(Call->getCallingConv()); |
| 468 NewCall->takeName(Call); |
| 469 FC->recordConvertedAndErase(Call, NewCall); |
| 470 } else if (// Handle these instructions as a convenience to allow |
| 471 // the pass to be used in more situations, even though we |
| 472 // don't expect them in PNaCl's stable ABI. |
| 473 isa<AllocaInst>(Inst) || |
| 474 isa<GetElementPtrInst>(Inst) || |
| 475 isa<VAArgInst>(Inst) || |
| 476 isa<IndirectBrInst>(Inst) || |
| 477 isa<ExtractValueInst>(Inst) || |
| 478 isa<InsertValueInst>(Inst) || |
| 479 // These atomics only operate on integer pointers, not |
| 480 // other pointers, so we don't need to recreate the |
| 481 // instruction. |
| 482 isa<AtomicCmpXchgInst>(Inst) || |
| 483 isa<AtomicRMWInst>(Inst)) { |
| 484 FC->convertInPlace(Inst); |
| 485 } |
| 486 } |
| 487 |
| 488 // Convert ptrtoint+inttoptr to a bitcast because it's shorter and |
| 489 // because some intrinsics work on bitcasts but not on |
| 490 // ptrtoint+inttoptr, in particular: |
| 491 // * llvm.lifetime.start/end (although we strip these out) |
| 492 // * llvm.eh.typeid.for |
| 493 static void SimplifyCasts(Instruction *Inst, Type *IntPtrType) { |
| 494 if (IntToPtrInst *Cast1 = dyn_cast<IntToPtrInst>(Inst)) { |
| 495 if (PtrToIntInst *Cast2 = dyn_cast<PtrToIntInst>(Cast1->getOperand(0))) { |
| 496 assert(Cast2->getType() == IntPtrType); |
| 497 Value *V = Cast2->getPointerOperand(); |
| 498 if (V->getType() != Cast1->getType()) |
| 499 V = new BitCastInst(V, Cast1->getType(), V->getName() + ".bc", Cast1); |
| 500 Cast1->replaceAllUsesWith(V); |
| 501 if (Cast1->use_empty()) |
| 502 Cast1->eraseFromParent(); |
| 503 if (Cast2->use_empty()) |
| 504 Cast2->eraseFromParent(); |
| 505 } |
| 506 } |
| 507 } |
| 508 |
| 509 static void CleanUpFunction(Function *Func, Type *IntPtrType) { |
| 510 // Remove the ptrtoint/bitcast ConstantExprs we introduced for |
| 511 // referencing globals. |
| 512 FunctionPass *Pass = createExpandConstantExprPass(); |
| 513 Pass->runOnFunction(*Func); |
| 514 delete Pass; |
| 515 |
| 516 for (Function::iterator BB = Func->begin(), E = Func->end(); |
| 517 BB != E; ++BB) { |
| 518 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); |
| 519 Iter != E; ) { |
| 520 SimplifyCasts(Iter++, IntPtrType); |
| 521 } |
| 522 } |
| 523 // Cleanup pass. |
| 524 for (Function::iterator BB = Func->begin(), E = Func->end(); |
| 525 BB != E; ++BB) { |
| 526 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); |
| 527 Iter != E; ) { |
| 528 Instruction *Inst = Iter++; |
| 529 // Add names to inttoptrs to make the output more readable. The |
| 530 // placeholder values get in the way of doing this earlier when |
| 531 // the inttoptrs are created. |
| 532 if (isa<IntToPtrInst>(Inst)) |
| 533 Inst->setName(Inst->getOperand(0)->getName() + ".asptr"); |
| 534 // Remove ptrtoints that were introduced for allocas but not used. |
| 535 if (isa<PtrToIntInst>(Inst) && Inst->use_empty()) |
| 536 Inst->eraseFromParent(); |
| 537 } |
| 538 } |
| 539 } |
| 540 |
| 541 char ReplacePtrsWithInts::ID = 0; |
| 542 INITIALIZE_PASS(ReplacePtrsWithInts, "replace-ptrs-with-ints", |
| 543 "Convert pointer values to integer values", |
| 544 false, false) |
| 545 |
| 546 bool ReplacePtrsWithInts::runOnModule(Module &M) { |
| 547 DataLayout DL(&M); |
| 548 DenseMap<const Function *, DISubprogram> FunctionDIs = makeSubprogramMap(M); |
| 549 Type *IntPtrType = DL.getIntPtrType(M.getContext()); |
| 550 |
| 551 for (Module::iterator Iter = M.begin(), E = M.end(); Iter != E; ) { |
| 552 Function *OldFunc = Iter++; |
| 553 // Intrinsics' types must be left alone. |
| 554 if (OldFunc->isIntrinsic()) |
| 555 continue; |
| 556 |
| 557 FunctionConverter FC(IntPtrType); |
| 558 FunctionType *NFTy = FC.convertFuncType(OldFunc->getFunctionType()); |
| 559 OldFunc->setAttributes(RemovePointerAttrs(M.getContext(), |
| 560 OldFunc->getAttributes())); |
| 561 Function *NewFunc = RecreateFunction(OldFunc, NFTy); |
| 562 |
| 563 // Move the arguments across to the new function. |
| 564 for (Function::arg_iterator Arg = OldFunc->arg_begin(), |
| 565 E = OldFunc->arg_end(), NewArg = NewFunc->arg_begin(); |
| 566 Arg != E; ++Arg, ++NewArg) { |
| 567 FC.recordConverted(Arg, NewArg); |
| 568 NewArg->takeName(Arg); |
| 569 } |
| 570 |
| 571 // invariant.end calls refer to invariant.start calls, so we must |
| 572 // remove the former first. |
| 573 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); |
| 574 BB != E; ++BB) { |
| 575 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); |
| 576 Iter != E; ) { |
| 577 if (IntrinsicInst *ICall = dyn_cast<IntrinsicInst>(Iter++)) { |
| 578 if (ICall->getIntrinsicID() == Intrinsic::invariant_end) |
| 579 ICall->eraseFromParent(); |
| 580 } |
| 581 } |
| 582 } |
| 583 |
| 584 // Convert the function body. |
| 585 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); |
| 586 BB != E; ++BB) { |
| 587 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); |
| 588 Iter != E; ) { |
| 589 ConvertInstruction(&DL, IntPtrType, &FC, Iter++); |
| 590 } |
| 591 } |
| 592 // Now that all the replacement instructions have been created, we |
| 593 // can update the debug intrinsic calls. |
| 594 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); |
| 595 BB != E; ++BB) { |
| 596 for (BasicBlock::iterator Inst = BB->begin(), E = BB->end(); |
| 597 Inst != E; ++Inst) { |
| 598 if (IntrinsicInst *Call = dyn_cast<IntrinsicInst>(Inst)) { |
| 599 if (Call->getIntrinsicID() == Intrinsic::dbg_declare) { |
| 600 ConvertMetadataOperand(&FC, Call, 0); |
| 601 } |
| 602 } |
| 603 } |
| 604 } |
| 605 FC.eraseReplacedInstructions(); |
| 606 |
| 607 // Patch the pointer to LLVM function in debug info descriptor. |
| 608 auto DI = FunctionDIs.find(OldFunc); |
| 609 if (DI != FunctionDIs.end()) |
| 610 DI->second.replaceFunction(NewFunc); |
| 611 |
| 612 OldFunc->eraseFromParent(); |
| 613 } |
| 614 // Now that all functions have their normalized types, we can remove |
| 615 // various casts. |
| 616 for (Module::iterator Func = M.begin(), E = M.end(); Func != E; ++Func) { |
| 617 CleanUpFunction(Func, IntPtrType); |
| 618 // Delete the now-unused bitcast ConstantExprs that we created so |
| 619 // that they don't interfere with StripDeadPrototypes. |
| 620 Func->removeDeadConstantUsers(); |
| 621 } |
| 622 return true; |
| 623 } |
| 624 |
| 625 ModulePass *llvm::createReplacePtrsWithIntsPass() { |
| 626 return new ReplacePtrsWithInts(); |
| 627 } |
OLD | NEW |