Chromium Code Reviews| 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 // The pass converts IR to the following normal form: | |
| 16 // | |
| 17 // All inttoptr and ptrtoint instructions use the same integer size | |
| 18 // (iPTR), so they do not implicitly truncate or zero-extend. | |
| 19 // | |
| 20 // alloca always allocates an i8 array type. | |
| 21 // | |
| 22 // Pointer types only appear in the following instructions: | |
| 23 // * loads and stores: the pointer operand is a NormalizedPtr. | |
| 24 // * function calls: the function operand is a NormalizedPtr. | |
| 25 // * intrinsic calls: any pointer arguments are NormalizedPtrs. | |
| 26 // * alloca | |
| 27 // * bitcast and inttoptr: only used as part of a NormalizedPtr. | |
| 28 // * ptrtoint: the operand is an InherentPtr. | |
| 29 // | |
| 30 // Where an InherentPtr is defined as a pointer value that is: | |
| 31 // * an alloca; | |
| 32 // * a GlobalValue (a function or global variable); or | |
| 33 // * an intrinsic call. | |
| 34 // | |
| 35 // And a NormalizedPtr is defined as a pointer value that is: | |
| 36 // * an inttoptr instruction; | |
| 37 // * an InherentPtr; or | |
| 38 // * a bitcast of an InherentPtr. | |
| 39 // | |
| 40 //===----------------------------------------------------------------------===// | |
| 41 | |
| 42 #include "llvm/ADT/DenseMap.h" | |
| 43 #include "llvm/IR/DataLayout.h" | |
| 44 #include "llvm/IR/DerivedTypes.h" | |
| 45 #include "llvm/IR/Function.h" | |
| 46 #include "llvm/IR/IRBuilder.h" | |
| 47 #include "llvm/IR/Instructions.h" | |
| 48 #include "llvm/IR/IntrinsicInst.h" | |
| 49 #include "llvm/IR/Module.h" | |
| 50 #include "llvm/IR/Type.h" | |
| 51 #include "llvm/Pass.h" | |
| 52 #include "llvm/Support/raw_ostream.h" | |
| 53 #include "llvm/Transforms/NaCl.h" | |
| 54 | |
| 55 using namespace llvm; | |
| 56 | |
| 57 namespace { | |
| 58 // This is a ModulePass because the pass must recreate functions in | |
| 59 // order to change their argument and return types. | |
| 60 struct ReplacePtrsWithInts : public ModulePass { | |
| 61 static char ID; // Pass identification, replacement for typeid | |
| 62 ReplacePtrsWithInts() : ModulePass(ID) { | |
| 63 initializeReplacePtrsWithIntsPass(*PassRegistry::getPassRegistry()); | |
| 64 } | |
| 65 | |
| 66 virtual bool runOnModule(Module &M); | |
| 67 }; | |
| 68 | |
| 69 // FunctionConverter stores the state for mapping old instructions | |
| 70 // (of pointer type) to converted instructions (of integer type) | |
| 71 // within a function, and provides methods for doing the conversion. | |
| 72 class FunctionConverter { | |
| 73 // Int type that pointer types are to be replaced with, typically i32. | |
| 74 Type *IntPtrType; | |
| 75 | |
| 76 struct RewrittenVal { | |
| 77 RewrittenVal(): IsPlaceholder(true), NewIntVal(NULL) {} | |
| 78 bool IsPlaceholder; | |
| 79 Value *NewIntVal; | |
| 80 }; | |
| 81 // Maps from old values (of pointer type) to converted values (of | |
| 82 // IntPtrType type). | |
| 83 DenseMap<Value *, RewrittenVal> RewriteMap; | |
| 84 // Number of placeholders; used for detecting unhandled cases. | |
| 85 int PlaceholderCount; | |
| 86 // List of instructions whose deletion has been deferred. | |
| 87 SmallVector<Instruction *, 20> ToErase; | |
| 88 | |
| 89 public: | |
| 90 FunctionConverter(Type *IntPtrType) | |
| 91 : IntPtrType(IntPtrType), PlaceholderCount(0) {} | |
| 92 | |
| 93 // Returns the normalized version of the given type, converting | |
| 94 // pointer types to IntPtrType. | |
| 95 Type *convertType(Type *Ty); | |
| 96 // Returns the normalized version of the given function type by | |
| 97 // normalizing the function's argument types. | |
| 98 FunctionType *convertFuncType(FunctionType *FTy); | |
| 99 | |
| 100 // Records that 'To' is the normalized version of 'From'. If 'To' | |
| 101 // is not of pointer type, no type conversion is required, so this | |
| 102 // can take the short cut of replacing 'To' with 'From'. | |
| 103 void recordConverted(Value *From, Value *To); | |
| 104 void recordConvertedAndErase(Instruction *From, Value *To); | |
| 105 | |
| 106 // Returns the normalized version of the given value. | |
| 107 Value *convert(Value *Val); | |
| 108 // Returns the NormalizedPtr form of the given pointer value. | |
| 109 // Inserts conversion instructions at InsertPt. | |
| 110 Value *convertBackToPtr(Value *Val, Instruction *InsertPt); | |
| 111 // Returns the NormalizedPtr form of the given function pointer. | |
| 112 // Inserts conversion instructions at InsertPt. | |
| 113 Value *convertFunctionPtr(Value *Callee, Instruction *InsertPt); | |
| 114 // Converts an instruction without recreating it, by wrapping its | |
| 115 // operands and result. | |
| 116 void convertInPlace(Instruction *Inst); | |
| 117 | |
| 118 void eraseReplacedInstructions(); | |
| 119 }; | |
| 120 } | |
| 121 | |
| 122 Type *FunctionConverter::convertType(Type *Ty) { | |
| 123 if (Ty->isPointerTy()) | |
| 124 return IntPtrType; | |
| 125 return Ty; | |
| 126 } | |
| 127 | |
| 128 FunctionType *FunctionConverter::convertFuncType(FunctionType *FTy) { | |
| 129 SmallVector<Type *, 8> ArgTypes; | |
| 130 for (FunctionType::param_iterator ArgTy = FTy->param_begin(), | |
| 131 E = FTy->param_end(); ArgTy != E; ++ArgTy) { | |
| 132 ArgTypes.push_back(convertType(*ArgTy)); | |
| 133 } | |
| 134 return FunctionType::get(convertType(FTy->getReturnType()), ArgTypes, | |
| 135 FTy->isVarArg()); | |
| 136 } | |
| 137 | |
| 138 void FunctionConverter::recordConverted(Value *From, Value *To) { | |
| 139 if (!From->getType()->isPointerTy()) { | |
| 140 From->replaceAllUsesWith(To); | |
| 141 return; | |
| 142 } | |
| 143 RewrittenVal *RV = &RewriteMap[From]; | |
| 144 if (RV->NewIntVal) { | |
| 145 assert(RV->IsPlaceholder); | |
| 146 // Resolve placeholder. | |
| 147 RV->NewIntVal->replaceAllUsesWith(To); | |
| 148 delete RV->NewIntVal; | |
| 149 RV->IsPlaceholder = false; | |
| 150 --PlaceholderCount; | |
| 151 } | |
| 152 RV->NewIntVal = To; | |
| 153 } | |
| 154 | |
| 155 void FunctionConverter::recordConvertedAndErase(Instruction *From, Value *To) { | |
| 156 recordConverted(From, To); | |
| 157 // There may still be references to this value, so defer deleting it. | |
| 158 ToErase.push_back(From); | |
| 159 } | |
| 160 | |
| 161 Value *FunctionConverter::convert(Value *Val) { | |
| 162 if (!Val->getType()->isPointerTy()) | |
| 163 return Val; | |
| 164 if (Constant *C = dyn_cast<Constant>(Val)) | |
| 165 return ConstantExpr::getPtrToInt(C, IntPtrType); | |
| 166 RewrittenVal *RV = &RewriteMap[Val]; | |
| 167 if (!RV->NewIntVal) { | |
| 168 // No converted value available yet, so create a placeholder. | |
| 169 Argument *Placeholder = new Argument(convertType(Val->getType())); | |
| 170 RV->NewIntVal = Placeholder; | |
| 171 ++PlaceholderCount; | |
| 172 } | |
| 173 return RV->NewIntVal; | |
| 174 } | |
| 175 | |
| 176 Value *FunctionConverter::convertBackToPtr(Value *Val, Instruction *InsertPt) { | |
| 177 Type *NewTy = | |
| 178 convertType(Val->getType()->getPointerElementType())->getPointerTo(); | |
| 179 Value *Conv = convert(Val); | |
| 180 return new IntToPtrInst(Conv, NewTy, Conv->getName() + ".asptr", InsertPt); | |
| 181 } | |
| 182 | |
| 183 Value *FunctionConverter::convertFunctionPtr(Value *Callee, | |
| 184 Instruction *InsertPt) { | |
| 185 FunctionType *FuncType = cast<FunctionType>( | |
| 186 Callee->getType()->getPointerElementType()); | |
| 187 Value *Conv = convert(Callee); | |
| 188 return new IntToPtrInst(Conv, convertFuncType(FuncType)->getPointerTo(), | |
| 189 Conv->getName() + ".asfuncptr", InsertPt); | |
| 190 } | |
| 191 | |
| 192 static bool ShouldLeaveAlone(Value *V) { | |
| 193 if (Function *F = dyn_cast<Function>(V)) | |
| 194 return F->isIntrinsic(); | |
| 195 if (isa<InlineAsm>(V)) | |
| 196 return true; | |
| 197 return false; | |
| 198 } | |
| 199 | |
| 200 void FunctionConverter::convertInPlace(Instruction *Inst) { | |
| 201 // Convert operands. | |
| 202 for (unsigned I = 0; I < Inst->getNumOperands(); ++I) { | |
| 203 Value *Arg = Inst->getOperand(I); | |
| 204 if (Arg->getType()->isPointerTy() && !ShouldLeaveAlone(Arg)) { | |
| 205 Value *Conv = convert(Arg); | |
| 206 Inst->setOperand(I, new IntToPtrInst(convert(Arg), Arg->getType(), | |
| 207 Conv->getName() + ".asptr", Inst)); | |
| 208 } | |
| 209 } | |
| 210 // Convert result. | |
| 211 if (Inst->getType()->isPointerTy()) { | |
| 212 Instruction *Cast = new PtrToIntInst( | |
| 213 Inst, convertType(Inst->getType()), Inst->getName() + ".asint"); | |
| 214 Cast->insertAfter(Inst); | |
| 215 recordConverted(Inst, Cast); | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 void FunctionConverter::eraseReplacedInstructions() { | |
| 220 if (PlaceholderCount) { | |
| 221 for (DenseMap<Value *, RewrittenVal>::iterator I = RewriteMap.begin(), | |
| 222 E = RewriteMap.end(); I != E; ++I) { | |
| 223 if (I->second.IsPlaceholder) | |
| 224 errs() << "Not converted: " << *I->first << "\n"; | |
| 225 } | |
| 226 report_fatal_error("Case not handled in ReplacePtrsWithInts"); | |
| 227 } | |
| 228 // We must do dropAllReferences() before doing eraseFromParent(), | |
| 229 // otherwise we will try to erase instructions that are still | |
| 230 // referenced. | |
| 231 for (SmallVectorImpl<Instruction *>::iterator I = ToErase.begin(), | |
| 232 E = ToErase.end(); | |
| 233 I != E; ++I) { | |
| 234 (*I)->dropAllReferences(); | |
| 235 } | |
| 236 for (SmallVectorImpl<Instruction *>::iterator I = ToErase.begin(), | |
| 237 E = ToErase.end(); | |
| 238 I != E; ++I) { | |
| 239 (*I)->eraseFromParent(); | |
| 240 } | |
| 241 } | |
| 242 | |
| 243 static void ConvertMetadataOperand(FunctionConverter *FC, | |
| 244 IntrinsicInst *Call, int Index) { | |
| 245 MDNode *MD = cast<MDNode>(Call->getArgOperand(Index)); | |
| 246 if (MD->getNumOperands() != 1) | |
| 247 return; | |
| 248 Value *MDArg = MD->getOperand(0); | |
| 249 if (MDArg && (isa<Argument>(MDArg) || isa<Instruction>(MDArg))) { | |
| 250 MDArg = FC->convert(MDArg); | |
| 251 if (PtrToIntInst *Cast = dyn_cast<PtrToIntInst>(MDArg)) { | |
| 252 // Unwrapping this is necessary for llvm.dbg.declare to work. | |
| 253 MDArg = Cast->getPointerOperand(); | |
| 254 } | |
| 255 SmallVector<Value *, 1> Args; | |
| 256 Args.push_back(MDArg); | |
| 257 Call->setArgOperand(Index, MDNode::get(Call->getContext(), Args)); | |
| 258 } | |
| 259 } | |
| 260 | |
| 261 static AttributeSet RemoveAttrs(LLVMContext &Context, AttributeSet Attrs) { | |
|
eliben
2013/05/22 16:08:02
Comment this function. Also, a more descriptive na
Mark Seaborn
2013/05/22 18:29:14
Done.
| |
| 262 SmallVector<AttributeSet, 8> AttrList; | |
| 263 for (unsigned Slot = 0; Slot < Attrs.getNumSlots(); ++Slot) { | |
| 264 unsigned Index = Attrs.getSlotIndex(Slot); | |
| 265 AttrBuilder AB; | |
| 266 for (AttributeSet::iterator Attr = Attrs.begin(Slot), E = Attrs.end(Slot); | |
| 267 Attr != E; ++Attr) { | |
| 268 switch (Attr->getKindAsEnum()) { | |
| 269 // ByVal and StructRet should already have been removed by the | |
| 270 // ExpandByVal pass. | |
| 271 case Attribute::ByVal: | |
| 272 case Attribute::StructRet: | |
| 273 case Attribute::Nest: | |
| 274 Attrs.dump(); | |
| 275 report_fatal_error("ReplacePtrsWithInts cannot handle " | |
| 276 "byval, sret or nest attrs"); | |
| 277 break; | |
| 278 // Strip NoCapture and NoAlias because they are only allowed | |
| 279 // on arguments of pointer type, and we are removing the | |
| 280 // pointer types. | |
| 281 case Attribute::NoCapture: | |
| 282 case Attribute::NoAlias: | |
| 283 break; | |
| 284 default: | |
| 285 AB.addAttribute(*Attr); | |
| 286 } | |
| 287 } | |
| 288 AttrList.push_back(AttributeSet::get(Context, Index, AB)); | |
| 289 } | |
| 290 return AttributeSet::get(Context, AttrList); | |
| 291 } | |
| 292 | |
| 293 template <class InstType> | |
| 294 static void CopyLoadOrStoreAttrs(InstType *Dest, InstType *Src) { | |
| 295 Dest->setVolatile(Src->isVolatile()); | |
| 296 Dest->setAlignment(Src->getAlignment()); | |
| 297 Dest->setOrdering(Src->getOrdering()); | |
| 298 Dest->setSynchScope(Src->getSynchScope()); | |
| 299 } | |
| 300 | |
| 301 static void ConvertInstruction(DataLayout *DL, Type *IntPtrType, | |
| 302 FunctionConverter *FC, Instruction *Inst) { | |
| 303 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Inst)) { | |
| 304 Value *Result = Ret->getReturnValue(); | |
| 305 if (Result) | |
| 306 Result = FC->convert(Result); | |
| 307 CopyDebug(ReturnInst::Create(Ret->getContext(), Result, Ret), Inst); | |
| 308 Ret->eraseFromParent(); | |
| 309 } else if (PHINode *Phi = dyn_cast<PHINode>(Inst)) { | |
| 310 PHINode *Phi2 = PHINode::Create(FC->convertType(Phi->getType()), | |
| 311 Phi->getNumIncomingValues(), | |
| 312 "", Phi); | |
| 313 CopyDebug(Phi2, Phi); | |
| 314 for (unsigned I = 0; I < Phi->getNumIncomingValues(); ++I) { | |
| 315 Phi2->addIncoming(FC->convert(Phi->getIncomingValue(I)), | |
| 316 Phi->getIncomingBlock(I)); | |
| 317 } | |
| 318 Phi2->takeName(Phi); | |
| 319 FC->recordConvertedAndErase(Phi, Phi2); | |
| 320 } else if (SelectInst *Op = dyn_cast<SelectInst>(Inst)) { | |
| 321 Instruction *Op2 = SelectInst::Create(Op->getCondition(), | |
| 322 FC->convert(Op->getTrueValue()), | |
| 323 FC->convert(Op->getFalseValue()), | |
| 324 "", Op); | |
| 325 CopyDebug(Op2, Op); | |
| 326 Op2->takeName(Op); | |
| 327 FC->recordConvertedAndErase(Op, Op2); | |
| 328 } else if (isa<PtrToIntInst>(Inst) || isa<IntToPtrInst>(Inst)) { | |
| 329 Value *Arg = FC->convert(Inst->getOperand(0)); | |
| 330 Type *ResultTy = FC->convertType(Inst->getType()); | |
| 331 IRBuilder<> Builder(Inst); | |
| 332 Builder.SetCurrentDebugLocation(Inst->getDebugLoc()); | |
| 333 Value *Result = Builder.CreateZExtOrTrunc(Arg, ResultTy, ""); | |
| 334 if (Result != Arg) | |
| 335 Result->takeName(Inst); | |
| 336 FC->recordConvertedAndErase(Inst, Result); | |
| 337 } else if (isa<BitCastInst>(Inst)) { | |
| 338 if (Inst->getType()->isPointerTy()) { | |
| 339 FC->recordConvertedAndErase(Inst, FC->convert(Inst->getOperand(0))); | |
| 340 } | |
| 341 } else if (ICmpInst *Cmp = dyn_cast<ICmpInst>(Inst)) { | |
| 342 Value *Cmp2 = CopyDebug(new ICmpInst(Inst, Cmp->getPredicate(), | |
| 343 FC->convert(Cmp->getOperand(0)), | |
| 344 FC->convert(Cmp->getOperand(1)), ""), | |
| 345 Inst); | |
| 346 Cmp2->takeName(Cmp); | |
| 347 Cmp->replaceAllUsesWith(Cmp2); | |
| 348 Cmp->eraseFromParent(); | |
| 349 } else if (LoadInst *Load = dyn_cast<LoadInst>(Inst)) { | |
| 350 Value *Ptr = FC->convertBackToPtr(Load->getPointerOperand(), Inst); | |
| 351 LoadInst *Result = new LoadInst(Ptr, "", Inst); | |
| 352 Result->takeName(Inst); | |
| 353 CopyDebug(Result, Inst); | |
| 354 CopyLoadOrStoreAttrs(Result, Load); | |
| 355 FC->recordConvertedAndErase(Inst, Result); | |
| 356 } else if (StoreInst *Store = dyn_cast<StoreInst>(Inst)) { | |
| 357 Value *Ptr = FC->convertBackToPtr(Store->getPointerOperand(), Inst); | |
| 358 StoreInst *Result = new StoreInst(FC->convert(Store->getValueOperand()), | |
| 359 Ptr, Inst); | |
| 360 CopyDebug(Result, Inst); | |
| 361 CopyLoadOrStoreAttrs(Result, Store); | |
| 362 Inst->eraseFromParent(); | |
| 363 } else if (CallInst *Call = dyn_cast<CallInst>(Inst)) { | |
| 364 if (IntrinsicInst *ICall = dyn_cast<IntrinsicInst>(Inst)) { | |
| 365 if (ICall->getIntrinsicID() == Intrinsic::lifetime_start || | |
| 366 ICall->getIntrinsicID() == Intrinsic::lifetime_end) { | |
| 367 // Remove alloca lifetime markers for now. This is because | |
|
Derek Schuff
2013/05/22 16:24:08
It's probably worth having this comment or some su
Mark Seaborn
2013/05/22 18:29:14
OK, added a small comment at the top.
| |
| 368 // the GVN pass can introduce lifetime markers taking PHI | |
| 369 // nodes as arguments. If ReplacePtrsWithInts converts the | |
| 370 // PHI node to int type, we will render those lifetime markers | |
| 371 // ineffective. But dropping a subset of lifetime markers is | |
| 372 // not safe in general. So, until LLVM better defines the | |
| 373 // semantics of lifetime markers, we drop them all. See: | |
| 374 // https://code.google.com/p/nativeclient/issues/detail?id=3443 | |
| 375 Inst->eraseFromParent(); | |
| 376 } else { | |
| 377 if (ICall->getIntrinsicID() == Intrinsic::dbg_declare) { | |
| 378 ConvertMetadataOperand(FC, ICall, 0); | |
| 379 } | |
| 380 FC->convertInPlace(Inst); | |
| 381 } | |
| 382 } else if (isa<InlineAsm>(Call->getCalledValue())) { | |
| 383 FC->convertInPlace(Inst); | |
| 384 } else { | |
| 385 SmallVector<Value *, 10> Args; | |
| 386 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
| 387 Args.push_back(FC->convert(Call->getArgOperand(I))); | |
| 388 CallInst *NewCall = CallInst::Create( | |
| 389 FC->convertFunctionPtr(Call->getCalledValue(), Call), | |
| 390 Args, "", Inst); | |
| 391 CopyDebug(NewCall, Call); | |
| 392 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
| 393 Call->getAttributes())); | |
| 394 NewCall->setCallingConv(Call->getCallingConv()); | |
| 395 NewCall->takeName(Call); | |
| 396 FC->recordConvertedAndErase(Call, NewCall); | |
| 397 } | |
| 398 } else if (InvokeInst *Call = dyn_cast<InvokeInst>(Inst)) { | |
| 399 SmallVector<Value *, 10> Args; | |
| 400 for (unsigned I = 0; I < Call->getNumArgOperands(); ++I) | |
| 401 Args.push_back(FC->convert(Call->getArgOperand(I))); | |
| 402 InvokeInst *NewCall = InvokeInst::Create( | |
| 403 FC->convertFunctionPtr(Call->getCalledValue(), Call), | |
| 404 Call->getNormalDest(), | |
| 405 Call->getUnwindDest(), | |
| 406 Args, "", Inst); | |
| 407 CopyDebug(NewCall, Call); | |
| 408 NewCall->setAttributes(RemoveAttrs(Call->getContext(), | |
| 409 Call->getAttributes())); | |
| 410 NewCall->setCallingConv(Call->getCallingConv()); | |
| 411 NewCall->takeName(Call); | |
| 412 FC->recordConvertedAndErase(Call, NewCall); | |
| 413 } else if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Inst)) { | |
| 414 Type *ElementTy = Inst->getType()->getPointerElementType(); | |
| 415 Type *ElementTy2 = ArrayType::get(Type::getInt8Ty(Inst->getContext()), | |
| 416 DL->getTypeAllocSize(ElementTy)); | |
| 417 unsigned Alignment = Alloca->getAlignment(); | |
| 418 if (Alignment == 0) | |
| 419 Alignment = DL->getPrefTypeAlignment(ElementTy); | |
| 420 Value *Tmp = CopyDebug(new AllocaInst(ElementTy2, Alloca->getArraySize(), | |
| 421 Alignment, "", Inst), | |
| 422 Inst); | |
| 423 Tmp->takeName(Alloca); | |
| 424 Value *Alloca2 = new PtrToIntInst(Tmp, IntPtrType, | |
| 425 Tmp->getName() + ".asint", Inst); | |
| 426 FC->recordConvertedAndErase(Alloca, Alloca2); | |
| 427 } else if (// These atomics only operate on integer pointers, not | |
| 428 // other pointers, so we don't need to recreate the | |
| 429 // instruction. | |
| 430 isa<AtomicCmpXchgInst>(Inst) || | |
| 431 isa<AtomicRMWInst>(Inst) || | |
| 432 // Handle these instructions as a convenience to allow | |
| 433 // the pass to be used in more situations, even though we | |
| 434 // don't expect them in PNaCl's stable ABI. | |
| 435 isa<GetElementPtrInst>(Inst) || | |
| 436 isa<VAArgInst>(Inst) || | |
| 437 isa<IndirectBrInst>(Inst) || | |
| 438 isa<ExtractValueInst>(Inst) || | |
| 439 isa<InsertValueInst>(Inst)) { | |
| 440 FC->convertInPlace(Inst); | |
| 441 } | |
| 442 } | |
| 443 | |
| 444 // Convert ptrtoint+inttoptr to a bitcast because it's shorter and | |
| 445 // because some intrinsics work on bitcasts but not on | |
| 446 // ptrtoint+inttoptr, in particular: | |
| 447 // * llvm.lifetime.start/end (although we strip these out) | |
| 448 // * llvm.eh.typeid.for | |
| 449 static void SimplifyCasts(Instruction *Inst, Type *IntPtrType) { | |
| 450 if (IntToPtrInst *Cast1 = dyn_cast<IntToPtrInst>(Inst)) { | |
| 451 if (PtrToIntInst *Cast2 = dyn_cast<PtrToIntInst>(Cast1->getOperand(0))) { | |
| 452 assert(Cast2->getType() == IntPtrType); | |
| 453 Value *V = Cast2->getPointerOperand(); | |
| 454 if (V->getType() != Cast1->getType()) | |
| 455 V = new BitCastInst(V, Cast1->getType(), V->getName() + ".bc", Cast1); | |
| 456 Cast1->replaceAllUsesWith(V); | |
| 457 if (Cast1->use_empty()) | |
| 458 Cast1->eraseFromParent(); | |
| 459 if (Cast2->use_empty()) | |
| 460 Cast2->eraseFromParent(); | |
| 461 } | |
| 462 } | |
| 463 } | |
| 464 | |
| 465 static void CleanUpFunction(Function *Func, Type *IntPtrType) { | |
| 466 // Remove the ptrtoint/bitcast ConstantExprs we introduced for | |
| 467 // referencing globals. | |
| 468 FunctionPass *Pass = createExpandConstantExprPass(); | |
| 469 Pass->runOnFunction(*Func); | |
| 470 delete Pass; | |
| 471 | |
| 472 for (Function::iterator BB = Func->begin(), E = Func->end(); | |
| 473 BB != E; ++BB) { | |
| 474 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 475 Iter != E; ) { | |
| 476 SimplifyCasts(Iter++, IntPtrType); | |
| 477 } | |
| 478 } | |
| 479 // Cleanup: Remove ptrtoints that were introduced for allocas but not used. | |
| 480 for (Function::iterator BB = Func->begin(), E = Func->end(); | |
| 481 BB != E; ++BB) { | |
| 482 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 483 Iter != E; ) { | |
| 484 Instruction *Inst = Iter++; | |
| 485 if (isa<PtrToIntInst>(Inst) && Inst->use_empty()) | |
| 486 Inst->eraseFromParent(); | |
| 487 } | |
| 488 } | |
| 489 } | |
| 490 | |
| 491 char ReplacePtrsWithInts::ID = 0; | |
| 492 INITIALIZE_PASS(ReplacePtrsWithInts, "replace-ptrs-with-ints", | |
| 493 "Convert pointer values to integer values", | |
| 494 false, false) | |
| 495 | |
| 496 bool ReplacePtrsWithInts::runOnModule(Module &M) { | |
| 497 DataLayout DL(&M); | |
| 498 Type *IntPtrType = DL.getIntPtrType(M.getContext()); | |
| 499 | |
| 500 for (Module::iterator Iter = M.begin(), E = M.end(); Iter != E; ) { | |
| 501 Function *OldFunc = Iter++; | |
| 502 // Intrinsics' types must be left alone. | |
| 503 if (OldFunc->isIntrinsic()) | |
| 504 continue; | |
| 505 | |
| 506 FunctionConverter FC(IntPtrType); | |
| 507 FunctionType *NFTy = FC.convertFuncType(OldFunc->getFunctionType()); | |
| 508 | |
| 509 // In order to change the function's argument types, we have to | |
| 510 // recreate the function. | |
| 511 Function *NewFunc = Function::Create(NFTy, OldFunc->getLinkage()); | |
| 512 NewFunc->copyAttributesFrom(OldFunc); | |
| 513 NewFunc->setAttributes(RemoveAttrs(M.getContext(), | |
| 514 NewFunc->getAttributes())); | |
| 515 M.getFunctionList().insert(OldFunc, NewFunc); | |
| 516 NewFunc->takeName(OldFunc); | |
| 517 NewFunc->getBasicBlockList().splice(NewFunc->begin(), | |
| 518 OldFunc->getBasicBlockList()); | |
| 519 | |
| 520 // Move the arguments across to the new function. | |
| 521 for (Function::arg_iterator Arg = OldFunc->arg_begin(), | |
| 522 E = OldFunc->arg_end(), NewArg = NewFunc->arg_begin(); | |
| 523 Arg != E; ++Arg, ++NewArg) { | |
| 524 FC.recordConverted(Arg, NewArg); | |
| 525 NewArg->takeName(Arg); | |
| 526 } | |
| 527 | |
| 528 // Convert the function body. | |
| 529 for (Function::iterator BB = NewFunc->begin(), E = NewFunc->end(); | |
| 530 BB != E; ++BB) { | |
| 531 for (BasicBlock::iterator Iter = BB->begin(), E = BB->end(); | |
| 532 Iter != E; ) { | |
| 533 ConvertInstruction(&DL, IntPtrType, &FC, Iter++); | |
| 534 } | |
| 535 } | |
| 536 FC.eraseReplacedInstructions(); | |
| 537 OldFunc->replaceAllUsesWith(ConstantExpr::getBitCast(NewFunc, | |
| 538 OldFunc->getType())); | |
| 539 OldFunc->eraseFromParent(); | |
| 540 } | |
| 541 // Now that all functions have their normalized types, we can remove | |
| 542 // various casts. | |
| 543 for (Module::iterator Func = M.begin(), E = M.end(); Func != E; ++Func) { | |
| 544 CleanUpFunction(Func, IntPtrType); | |
| 545 } | |
| 546 return true; | |
| 547 } | |
| 548 | |
| 549 ModulePass *llvm::createReplacePtrsWithIntsPass() { | |
| 550 return new ReplacePtrsWithInts(); | |
| 551 } | |
| OLD | NEW |