OLD | NEW |
(Empty) | |
| 1 //===- SimplifyStructRegSignatures.cpp - struct regs to struct pointers----===// |
| 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 replaces function signatures exposing struct registers |
| 11 // to byval pointer-based signatures. |
| 12 // |
| 13 // There are 2 types of signatures that are thus changed: |
| 14 // |
| 15 // @foo(%some_struct %val) -> @foo(%some_struct* byval %val) |
| 16 // and |
| 17 // %someStruct @bar(<other_args>) -> void @bar(%someStruct* sret, <other_args>) |
| 18 // |
| 19 // Such function types may appear in other type declarations, for example: |
| 20 // |
| 21 // %a_struct = type { void (%some_struct)*, i32 } |
| 22 // |
| 23 // We map such types to corresponding types, mapping the function types |
| 24 // appropriately: |
| 25 // |
| 26 // %a_struct.0 = type { void (%some_struct*)*, i32 } |
| 27 //===----------------------------------------------------------------------===// |
| 28 |
| 29 #include "llvm/ADT/SmallString.h" |
| 30 #include "llvm/IR/IRBuilder.h" |
| 31 #include "llvm/ADT/ArrayRef.h" |
| 32 #include "llvm/ADT/DenseSet.h" |
| 33 #include "llvm/ADT/ilist.h" |
| 34 #include "llvm/ADT/SetVector.h" |
| 35 #include "llvm/ADT/SmallVector.h" |
| 36 #include "llvm/ADT/Twine.h" |
| 37 #include "llvm/IR/Argument.h" |
| 38 #include "llvm/IR/Attributes.h" |
| 39 #include "llvm/IR/BasicBlock.h" |
| 40 #include "llvm/IR/DebugInfo.h" |
| 41 #include "llvm/IR/DerivedTypes.h" |
| 42 #include "llvm/IR/Function.h" |
| 43 #include "llvm/IR/GlobalValue.h" |
| 44 #include "llvm/IR/Instructions.h" |
| 45 #include "llvm/IR/Module.h" |
| 46 #include "llvm/IR/Type.h" |
| 47 #include "llvm/IR/Use.h" |
| 48 #include "llvm/IR/User.h" |
| 49 #include "llvm/IR/Value.h" |
| 50 #include "llvm/Pass.h" |
| 51 #include "llvm/PassInfo.h" |
| 52 #include "llvm/PassRegistry.h" |
| 53 #include "llvm/PassSupport.h" |
| 54 #include "llvm/Transforms/NaCl.h" |
| 55 #include "llvm/Support/Debug.h" |
| 56 |
| 57 #include <cassert> |
| 58 #include <cstddef> |
| 59 |
| 60 using namespace llvm; |
| 61 |
| 62 namespace { |
| 63 |
| 64 static const unsigned int TypicalFuncArity = 8; |
| 65 static const unsigned int TypicalStructArity = 8; |
| 66 |
| 67 class MappingResult { |
| 68 public: |
| 69 MappingResult(Type *ATy, bool Chg) { |
| 70 Ty = ATy; |
| 71 Changed = Chg; |
| 72 } |
| 73 |
| 74 bool isChanged() { return Changed; } |
| 75 |
| 76 Type *operator->() { return Ty; } |
| 77 |
| 78 operator Type *() { return Ty; } |
| 79 |
| 80 private: |
| 81 Type *Ty; |
| 82 bool Changed; |
| 83 }; |
| 84 |
| 85 // Utility class. For any given type, get the associated type that is free of |
| 86 // struct register arguments. |
| 87 class TypeMapper { |
| 88 public: |
| 89 typedef DenseMap<StructType *, StructType *> StructMap; |
| 90 Type *getSimpleType(LLVMContext &Ctx, Type *Ty); |
| 91 |
| 92 private: |
| 93 DenseMap<Type *, Type *> MappedTypes; |
| 94 MappingResult getSimpleArgumentType(LLVMContext &Ctx, Type *Ty, |
| 95 StructMap &Tentatives); |
| 96 MappingResult getSimpleAggregateTypeInternal(LLVMContext &Ctx, Type *Ty, |
| 97 StructMap &Tentatives); |
| 98 |
| 99 bool isChangedStruct(LLVMContext &Ctx, StructType *StructTy, |
| 100 SmallVector<Type *, TypicalStructArity> &ElemTypes, |
| 101 StructMap &Tentatives); |
| 102 }; |
| 103 |
| 104 // This is a ModulePass because the pass recreates functions in |
| 105 // order to change their signatures. |
| 106 class SimplifyStructRegSignatures : public ModulePass { |
| 107 public: |
| 108 static char ID; |
| 109 |
| 110 SimplifyStructRegSignatures() : ModulePass(ID) { |
| 111 initializeSimplifyStructRegSignaturesPass(*PassRegistry::getPassRegistry()); |
| 112 } |
| 113 virtual bool runOnModule(Module &M); |
| 114 |
| 115 private: |
| 116 TypeMapper Mapper; |
| 117 DenseSet<Function *> FunctionsToDelete; |
| 118 SetVector<CallInst *> CallsToPatch; |
| 119 SetVector<InvokeInst *> InvokesToPatch; |
| 120 DenseMap<Function *, Function *> FunctionMap; |
| 121 bool |
| 122 simplifyFunction(LLVMContext &Ctx, Function *OldFunc, |
| 123 DenseMap<const Function *, DISubprogram> &DISubprogramMap); |
| 124 void scheduleCallsForCleanup(Function *NewFunc); |
| 125 template <class TCall> |
| 126 void fixCallSite(LLVMContext &Ctx, TCall *Call, unsigned PreferredAlignment); |
| 127 void fixFunctionBody(LLVMContext &Ctx, Function *OldFunc, Function *NewFunc); |
| 128 |
| 129 template <class TCall> |
| 130 TCall *fixCallTargetAndArguments(LLVMContext &Ctx, IRBuilder<> &Builder, |
| 131 TCall *OldCall, Value *NewTarget, |
| 132 FunctionType *NewType, |
| 133 Value *ExtraArg = nullptr); |
| 134 }; |
| 135 } |
| 136 |
| 137 char SimplifyStructRegSignatures::ID = 0; |
| 138 |
| 139 INITIALIZE_PASS( |
| 140 SimplifyStructRegSignatures, "simplify-struct-reg-signatures", |
| 141 "Simplify function signatures by removing struct register parameters", |
| 142 false, false) |
| 143 |
| 144 // The type is "simple" if it does not recursively reference a |
| 145 // function type with at least an operand (arg or return) typed as struct |
| 146 // register. |
| 147 Type *TypeMapper::getSimpleType(LLVMContext &Ctx, Type *Ty) { |
| 148 auto Found = MappedTypes.find(Ty); |
| 149 if (Found != MappedTypes.end()) { |
| 150 return Found->second; |
| 151 } |
| 152 |
| 153 StructMap Tentatives; |
| 154 auto Ret = getSimpleAggregateTypeInternal(Ctx, Ty, Tentatives); |
| 155 assert(Tentatives.size() == 0); |
| 156 |
| 157 if (!Ty->isStructTy()) { |
| 158 // Structs are memoized in getSimpleAggregateTypeInternal. |
| 159 MappedTypes[Ty] = Ret; |
| 160 } |
| 161 return Ret; |
| 162 } |
| 163 |
| 164 // Transforms any type that could transitively reference a function pointer |
| 165 // into a simplified type. |
| 166 // We enter this function trying to determine the mapping of a type. Because |
| 167 // of how structs are handled (not interned by llvm - see further comments |
| 168 // below) we may be working with temporary types - types (pointers, for example) |
| 169 // transitively referencing "tentative" structs. For that reason, we do not |
| 170 // memoize anything here, except for structs. The latter is so that we avoid |
| 171 // unnecessary repeated creation of types (pointers, function types, etc), |
| 172 // as we try to map a given type. |
| 173 MappingResult |
| 174 TypeMapper::getSimpleAggregateTypeInternal(LLVMContext &Ctx, Type *Ty, |
| 175 StructMap &Tentatives) { |
| 176 // Leverage the map for types we encounter on the way. |
| 177 auto Found = MappedTypes.find(Ty); |
| 178 if (Found != MappedTypes.end()) { |
| 179 return {Found->second, Found->second != Ty}; |
| 180 } |
| 181 |
| 182 if (auto *OldFnTy = dyn_cast<FunctionType>(Ty)) { |
| 183 Type *OldRetType = OldFnTy->getReturnType(); |
| 184 Type *NewRetType = OldRetType; |
| 185 Type *Void = Type::getVoidTy(Ctx); |
| 186 SmallVector<Type *, TypicalFuncArity> NewArgs; |
| 187 bool Changed = false; |
| 188 // Struct register returns become the first parameter of the new FT. |
| 189 // The new FT has void for the return type |
| 190 if (OldRetType->isAggregateType()) { |
| 191 NewRetType = Void; |
| 192 Changed = true; |
| 193 NewArgs.push_back(getSimpleArgumentType(Ctx, OldRetType, Tentatives)); |
| 194 } |
| 195 for (auto OldParam : OldFnTy->params()) { |
| 196 auto NewType = getSimpleArgumentType(Ctx, OldParam, Tentatives); |
| 197 Changed |= NewType.isChanged(); |
| 198 NewArgs.push_back(NewType); |
| 199 } |
| 200 Type *NewFuncType = |
| 201 FunctionType::get(NewRetType, NewArgs, OldFnTy->isVarArg()); |
| 202 return {NewFuncType, Changed}; |
| 203 } |
| 204 |
| 205 if (auto PtrTy = dyn_cast<PointerType>(Ty)) { |
| 206 auto NewTy = getSimpleAggregateTypeInternal( |
| 207 Ctx, PtrTy->getPointerElementType(), Tentatives); |
| 208 |
| 209 return {NewTy->getPointerTo(PtrTy->getAddressSpace()), NewTy.isChanged()}; |
| 210 } |
| 211 |
| 212 if (auto ArrTy = dyn_cast<ArrayType>(Ty)) { |
| 213 auto NewTy = getSimpleAggregateTypeInternal( |
| 214 Ctx, ArrTy->getArrayElementType(), Tentatives); |
| 215 return {ArrayType::get(NewTy, ArrTy->getArrayNumElements()), |
| 216 NewTy.isChanged()}; |
| 217 } |
| 218 |
| 219 if (auto VecTy = dyn_cast<VectorType>(Ty)) { |
| 220 auto NewTy = getSimpleAggregateTypeInternal( |
| 221 Ctx, VecTy->getVectorElementType(), Tentatives); |
| 222 return {VectorType::get(NewTy, VecTy->getVectorNumElements()), |
| 223 NewTy.isChanged()}; |
| 224 } |
| 225 |
| 226 // LLVM doesn't intern identified structs (the ones with a name). This, |
| 227 // together with the fact that such structs can be recursive, |
| 228 // complicates things a bit. We want to make sure that we only change |
| 229 // "unsimplified" structs (those that somehow reference funcs that |
| 230 // are not simple). |
| 231 // We don't want to change "simplified" structs, otherwise converting |
| 232 // instruction types will become trickier. |
| 233 if (auto StructTy = dyn_cast<StructType>(Ty)) { |
| 234 SmallVector<Type *, TypicalStructArity> ElemTypes; |
| 235 if (!StructTy->isLiteral()) { |
| 236 // Literals - struct without a name - cannot be recursive, so we |
| 237 // don't need to form tentatives. |
| 238 auto Found = Tentatives.find(StructTy); |
| 239 |
| 240 // Having a tentative means we are in a recursion trying to map this |
| 241 // particular struct, so arriving back to it is not a change. |
| 242 // We will determine if this struct is actually |
| 243 // changed by checking its other fields. |
| 244 if (Found != Tentatives.end()) { |
| 245 return {Found->second, false}; |
| 246 } |
| 247 // We have never seen this struct, so we start a tentative. |
| 248 std::string NewName = StructTy->getStructName(); |
| 249 NewName += ".simplified"; |
| 250 StructType *Tentative = StructType::create(Ctx, NewName); |
| 251 Tentatives[StructTy] = Tentative; |
| 252 |
| 253 bool Changed = isChangedStruct(Ctx, StructTy, ElemTypes, Tentatives); |
| 254 |
| 255 Tentatives.erase(StructTy); |
| 256 // We can now decide the mapping of the struct. We will register it |
| 257 // early with MappedTypes, to avoid leaking tentatives unnecessarily. |
| 258 // We are leaking the created struct here, but there is no way to |
| 259 // correctly delete it. |
| 260 if (!Changed) { |
| 261 return {MappedTypes[StructTy] = StructTy, false}; |
| 262 } else { |
| 263 Tentative->setBody(ElemTypes, StructTy->isPacked()); |
| 264 return {MappedTypes[StructTy] = Tentative, true}; |
| 265 } |
| 266 } else { |
| 267 bool Changed = isChangedStruct(Ctx, StructTy, ElemTypes, Tentatives); |
| 268 return {MappedTypes[StructTy] = |
| 269 StructType::get(Ctx, ElemTypes, StructTy->isPacked()), |
| 270 Changed}; |
| 271 } |
| 272 } |
| 273 |
| 274 // Anything else stays the same. |
| 275 return {Ty, false}; |
| 276 } |
| 277 |
| 278 bool TypeMapper::isChangedStruct( |
| 279 LLVMContext &Ctx, StructType *StructTy, |
| 280 SmallVector<Type *, TypicalStructArity> &ElemTypes, StructMap &Tentatives) { |
| 281 bool Changed = false; |
| 282 unsigned StructElemCount = StructTy->getStructNumElements(); |
| 283 for (unsigned I = 0; I < StructElemCount; I++) { |
| 284 auto NewElem = getSimpleAggregateTypeInternal( |
| 285 Ctx, StructTy->getStructElementType(I), Tentatives); |
| 286 ElemTypes.push_back(NewElem); |
| 287 Changed |= NewElem.isChanged(); |
| 288 } |
| 289 return Changed; |
| 290 } |
| 291 |
| 292 // Get the simplified type of a function argument. |
| 293 MappingResult TypeMapper::getSimpleArgumentType(LLVMContext &Ctx, Type *Ty, |
| 294 StructMap &Tentatives) { |
| 295 // struct registers become pointers to simple structs |
| 296 if (Ty->isAggregateType()) { |
| 297 return MappingResult( |
| 298 PointerType::get(getSimpleAggregateTypeInternal(Ctx, Ty, Tentatives), |
| 299 0), |
| 300 true); |
| 301 } |
| 302 |
| 303 return getSimpleAggregateTypeInternal(Ctx, Ty, Tentatives); |
| 304 } |
| 305 |
| 306 // Apply 'byval' to func arguments that used to be struct regs. |
| 307 // Apply 'sret' to the argument corresponding to the return in the old |
| 308 // signature. |
| 309 static void ApplyByValAndSRet(Function *OldFunc, Function *NewFunc) { |
| 310 // When calling addAttribute, the first one refers to the function, so we |
| 311 // skip past that. |
| 312 unsigned ArgOffset = 1; |
| 313 if (OldFunc->getReturnType()->isAggregateType()) { |
| 314 NewFunc->addAttribute(1, Attribute::AttrKind::StructRet); |
| 315 ArgOffset++; |
| 316 } |
| 317 |
| 318 auto &NewArgList = NewFunc->getArgumentList(); |
| 319 auto NewArg = NewArgList.begin(); |
| 320 for (const Argument &OldArg : OldFunc->getArgumentList()) { |
| 321 if (OldArg.getType()->isAggregateType()) { |
| 322 NewFunc->addAttribute(NewArg->getArgNo() + ArgOffset, |
| 323 Attribute::AttrKind::ByVal); |
| 324 } |
| 325 NewArg++; |
| 326 } |
| 327 } |
| 328 |
| 329 // Update the arg names for a newly created function. |
| 330 static void UpdateArgNames(Function *OldFunc, Function *NewFunc) { |
| 331 auto NewArgIter = NewFunc->arg_begin(); |
| 332 if (OldFunc->getReturnType()->isAggregateType()) { |
| 333 NewArgIter->setName("retVal"); |
| 334 NewArgIter++; |
| 335 } |
| 336 |
| 337 for (const Argument &OldArg : OldFunc->args()) { |
| 338 Argument *NewArg = NewArgIter++; |
| 339 NewArg->setName(OldArg.getName() + |
| 340 (OldArg.getType()->isAggregateType() ? ".ptr" : "")); |
| 341 } |
| 342 } |
| 343 |
| 344 // Replace all uses of an old value with a new one, disregarding the type. We |
| 345 // correct the types after we wire the new parameters in, in fixFunctionBody. |
| 346 static void BlindReplace(Value *Old, Value *New) { |
| 347 for (auto UseIter = Old->use_begin(), E = Old->use_end(); E != UseIter;) { |
| 348 Use &AUse = *(UseIter++); |
| 349 AUse.set(New); |
| 350 } |
| 351 } |
| 352 |
| 353 // Adapt the body of a function for the new arguments. |
| 354 static void ConvertArgumentValue(Value *Old, Value *New, |
| 355 Instruction *InsPoint) { |
| 356 if (Old == New) |
| 357 return; |
| 358 |
| 359 if (Old->getType() == New->getType()) { |
| 360 Old->replaceAllUsesWith(New); |
| 361 New->takeName(Old); |
| 362 return; |
| 363 } |
| 364 |
| 365 bool IsAggregateToPtr = |
| 366 Old->getType()->isAggregateType() && New->getType()->isPointerTy(); |
| 367 BlindReplace(Old, (IsAggregateToPtr |
| 368 ? new LoadInst(New, Old->getName() + ".sreg", InsPoint) |
| 369 : New)); |
| 370 } |
| 371 |
| 372 // Fix returns. Return true if fixes were needed. |
| 373 static void FixReturn(Function *OldFunc, Function *NewFunc) { |
| 374 |
| 375 Argument *FirstNewArg = NewFunc->getArgumentList().begin(); |
| 376 |
| 377 for (auto BIter = NewFunc->begin(), LastBlock = NewFunc->end(); |
| 378 LastBlock != BIter;) { |
| 379 BasicBlock *BB = BIter++; |
| 380 for (auto IIter = BB->begin(), LastI = BB->end(); LastI != IIter;) { |
| 381 Instruction *Instr = IIter++; |
| 382 if (ReturnInst *Ret = dyn_cast<ReturnInst>(Instr)) { |
| 383 auto RetVal = Ret->getReturnValue(); |
| 384 IRBuilder<> Builder(Ret); |
| 385 StoreInst *Store = Builder.CreateStore(RetVal, FirstNewArg); |
| 386 Store->setAlignment(FirstNewArg->getParamAlignment()); |
| 387 Builder.CreateRetVoid(); |
| 388 Ret->eraseFromParent(); |
| 389 } |
| 390 } |
| 391 } |
| 392 } |
| 393 |
| 394 // TODO (mtrofin): is this comprehensive? |
| 395 template <class TCall> |
| 396 void CopyCallAttributesAndMetadata(TCall *Orig, TCall *NewCall) { |
| 397 NewCall->setCallingConv(Orig->getCallingConv()); |
| 398 NewCall->setAttributes(NewCall->getAttributes().addAttributes( |
| 399 Orig->getContext(), AttributeSet::FunctionIndex, |
| 400 Orig->getAttributes().getFnAttributes())); |
| 401 NewCall->takeName(Orig); |
| 402 } |
| 403 |
| 404 static InvokeInst *CreateCallFrom(InvokeInst *Orig, Value *Target, |
| 405 ArrayRef<Value *> &Args, |
| 406 IRBuilder<> &Builder) { |
| 407 auto Ret = Builder.CreateInvoke(Target, Orig->getNormalDest(), |
| 408 Orig->getUnwindDest(), Args); |
| 409 CopyCallAttributesAndMetadata(Orig, Ret); |
| 410 return Ret; |
| 411 } |
| 412 |
| 413 static CallInst *CreateCallFrom(CallInst *Orig, Value *Target, |
| 414 ArrayRef<Value *> &Args, IRBuilder<> &Builder) { |
| 415 |
| 416 CallInst *Ret = Builder.CreateCall(Target, Args); |
| 417 Ret->setTailCallKind(Orig->getTailCallKind()); |
| 418 CopyCallAttributesAndMetadata(Orig, Ret); |
| 419 return Ret; |
| 420 } |
| 421 |
| 422 // Fix a call site by handing return type changes and/or parameter type and |
| 423 // attribute changes. |
| 424 template <class TCall> |
| 425 void SimplifyStructRegSignatures::fixCallSite(LLVMContext &Ctx, TCall *OldCall, |
| 426 unsigned PreferredAlignment) { |
| 427 Value *NewTarget = OldCall->getCalledValue(); |
| 428 |
| 429 if (Function *CalledFunc = dyn_cast<Function>(NewTarget)) { |
| 430 NewTarget = this->FunctionMap[CalledFunc]; |
| 431 } |
| 432 assert(NewTarget); |
| 433 |
| 434 auto *NewType = cast<FunctionType>( |
| 435 Mapper.getSimpleType(Ctx, NewTarget->getType())->getPointerElementType()); |
| 436 |
| 437 auto *OldRetType = OldCall->getType(); |
| 438 const bool IsSRet = |
| 439 !OldCall->getType()->isVoidTy() && NewType->getReturnType()->isVoidTy(); |
| 440 |
| 441 IRBuilder<> Builder(OldCall); |
| 442 if (IsSRet) { |
| 443 auto *Alloca = Builder.CreateAlloca(OldRetType); |
| 444 Alloca->takeName(OldCall); |
| 445 Alloca->setAlignment(PreferredAlignment); |
| 446 |
| 447 auto *NewCall = fixCallTargetAndArguments(Ctx, Builder, OldCall, NewTarget, |
| 448 NewType, Alloca); |
| 449 assert(NewCall); |
| 450 auto *Load = Builder.CreateLoad(Alloca, Alloca->getName() + ".sreg"); |
| 451 Load->setAlignment(Alloca->getAlignment()); |
| 452 OldCall->replaceAllUsesWith(Load); |
| 453 } else { |
| 454 auto *NewCall = |
| 455 fixCallTargetAndArguments(Ctx, Builder, OldCall, NewTarget, NewType); |
| 456 OldCall->replaceAllUsesWith(NewCall); |
| 457 } |
| 458 |
| 459 OldCall->eraseFromParent(); |
| 460 } |
| 461 |
| 462 template <class TCall> |
| 463 TCall *SimplifyStructRegSignatures::fixCallTargetAndArguments( |
| 464 LLVMContext &Ctx, IRBuilder<> &Builder, TCall *OldCall, Value *NewTarget, |
| 465 FunctionType *NewType, Value *ExtraArg) { |
| 466 SmallSetVector<unsigned, TypicalFuncArity> ByRefPlaces; |
| 467 SmallVector<Value *, TypicalFuncArity> NewArgs; |
| 468 |
| 469 unsigned argOffset = ExtraArg ? 1 : 0; |
| 470 if (ExtraArg) |
| 471 NewArgs.push_back(ExtraArg); |
| 472 |
| 473 for (unsigned ArgPos = 0; |
| 474 ArgPos < NewType->getFunctionNumParams() - argOffset; ArgPos++) { |
| 475 |
| 476 Use &OldArgUse = OldCall->getOperandUse(ArgPos); |
| 477 Value *OldArg = OldArgUse; |
| 478 Type *OldArgType = OldArg->getType(); |
| 479 unsigned NewArgPos = OldArgUse.getOperandNo() + argOffset; |
| 480 Type *NewArgType = NewType->getFunctionParamType(NewArgPos); |
| 481 |
| 482 if (OldArgType != NewArgType && OldArgType->isAggregateType()) { |
| 483 AllocaInst *Alloca = |
| 484 Builder.CreateAlloca(OldArgType, nullptr, OldArg->getName() + ".ptr"); |
| 485 Builder.CreateStore(OldArg, Alloca); |
| 486 ByRefPlaces.insert(NewArgPos); |
| 487 NewArgs.push_back(Alloca); |
| 488 } else { |
| 489 NewArgs.push_back(OldArg); |
| 490 } |
| 491 } |
| 492 |
| 493 ArrayRef<Value *> ArrRef = NewArgs; |
| 494 TCall *NewCall = CreateCallFrom(OldCall, NewTarget, ArrRef, Builder); |
| 495 |
| 496 // Copy the attributes over, and add byref/sret as necessary. |
| 497 const AttributeSet &OldAttrSet = OldCall->getAttributes(); |
| 498 const AttributeSet &NewAttrSet = NewCall->getAttributes(); |
| 499 |
| 500 for (unsigned I = 0; I < NewCall->getNumArgOperands(); I++) { |
| 501 NewCall->setAttributes(NewAttrSet.addAttributes( |
| 502 Ctx, I + argOffset + 1, OldAttrSet.getParamAttributes(I + 1))); |
| 503 if (ByRefPlaces.count(I)) { |
| 504 NewCall->addAttribute(I + 1, Attribute::ByVal); |
| 505 } |
| 506 } |
| 507 |
| 508 if (ExtraArg) { |
| 509 NewAttrSet.addAttributes(Ctx, 1, OldAttrSet.getRetAttributes()); |
| 510 NewCall->addAttribute(1, Attribute::StructRet); |
| 511 } else { |
| 512 NewCall->setAttributes(NewAttrSet.addAttributes( |
| 513 Ctx, AttributeSet::ReturnIndex, OldAttrSet.getRetAttributes())); |
| 514 } |
| 515 return NewCall; |
| 516 } |
| 517 |
| 518 void SimplifyStructRegSignatures::scheduleCallsForCleanup(Function *NewFunc) { |
| 519 for (auto &BBIter : NewFunc->getBasicBlockList()) { |
| 520 for (auto &IIter : BBIter.getInstList()) { |
| 521 if (CallInst *Call = dyn_cast<CallInst>(&IIter)) { |
| 522 CallsToPatch.insert(Call); |
| 523 } else if (InvokeInst *Invoke = dyn_cast<InvokeInst>(&IIter)) { |
| 524 InvokesToPatch.insert(Invoke); |
| 525 } |
| 526 } |
| 527 } |
| 528 } |
| 529 |
| 530 // Change function body in the light of type changes. |
| 531 void SimplifyStructRegSignatures::fixFunctionBody(LLVMContext &Ctx, |
| 532 Function *OldFunc, |
| 533 Function *NewFunc) { |
| 534 if (NewFunc->empty()) |
| 535 return; |
| 536 |
| 537 bool returnWasFixed = OldFunc->getReturnType()->isAggregateType(); |
| 538 |
| 539 Instruction *InsPoint = NewFunc->begin()->begin(); |
| 540 auto NewArgIter = NewFunc->arg_begin(); |
| 541 // Advance one more if we used to return a struct register. |
| 542 if (returnWasFixed) |
| 543 NewArgIter++; |
| 544 |
| 545 // Wire new parameters in. |
| 546 for (auto ArgIter = OldFunc->arg_begin(), E = OldFunc->arg_end(); |
| 547 E != ArgIter;) { |
| 548 Argument *OldArg = ArgIter++; |
| 549 Argument *NewArg = NewArgIter++; |
| 550 ConvertArgumentValue(OldArg, NewArg, InsPoint); |
| 551 } |
| 552 |
| 553 // Now fix instruction types. We know that each value could only possibly be |
| 554 // of a simplified type. At the end of this, call sites will be invalid, but |
| 555 // we handle that afterwards, to make sure we have all the functions changed |
| 556 // first (so that calls have valid targets) |
| 557 for (auto BBIter = NewFunc->begin(), LBlock = NewFunc->end(); |
| 558 LBlock != BBIter;) { |
| 559 auto Block = BBIter++; |
| 560 for (auto IIter = Block->begin(), LIns = Block->end(); LIns != IIter;) { |
| 561 auto Instr = IIter++; |
| 562 Instr->mutateType(Mapper.getSimpleType(Ctx, Instr->getType())); |
| 563 } |
| 564 } |
| 565 if (returnWasFixed) |
| 566 FixReturn(OldFunc, NewFunc); |
| 567 } |
| 568 |
| 569 // Ensure function is simplified, returning true if the function |
| 570 // had to be changed. |
| 571 bool SimplifyStructRegSignatures::simplifyFunction( |
| 572 LLVMContext &Ctx, Function *OldFunc, |
| 573 DenseMap<const Function *, DISubprogram> &DISubprogramMap) { |
| 574 auto *OldFT = OldFunc->getFunctionType(); |
| 575 auto *NewFT = cast<FunctionType>(Mapper.getSimpleType(Ctx, OldFT)); |
| 576 |
| 577 Function *&AssociatedFctLoc = FunctionMap[OldFunc]; |
| 578 if (NewFT != OldFT) { |
| 579 auto *NewFunc = Function::Create(NewFT, OldFunc->getLinkage()); |
| 580 AssociatedFctLoc = NewFunc; |
| 581 |
| 582 NewFunc->copyAttributesFrom(OldFunc); |
| 583 OldFunc->getParent()->getFunctionList().insert(OldFunc, NewFunc); |
| 584 NewFunc->takeName(OldFunc); |
| 585 |
| 586 UpdateArgNames(OldFunc, NewFunc); |
| 587 ApplyByValAndSRet(OldFunc, NewFunc); |
| 588 |
| 589 NewFunc->getBasicBlockList().splice(NewFunc->begin(), |
| 590 OldFunc->getBasicBlockList()); |
| 591 |
| 592 fixFunctionBody(Ctx, OldFunc, NewFunc); |
| 593 FunctionsToDelete.insert(OldFunc); |
| 594 auto Found = DISubprogramMap.find(OldFunc); |
| 595 if (Found != DISubprogramMap.end()) |
| 596 Found->second.replaceFunction(NewFunc); |
| 597 } else { |
| 598 AssociatedFctLoc = OldFunc; |
| 599 } |
| 600 scheduleCallsForCleanup(AssociatedFctLoc); |
| 601 return NewFT != OldFT; |
| 602 } |
| 603 |
| 604 bool SimplifyStructRegSignatures::runOnModule(Module &M) { |
| 605 bool Changed = false; |
| 606 |
| 607 const DataLayout *DL = M.getDataLayout(); |
| 608 unsigned PreferredAlignment = 0; |
| 609 if (DL) |
| 610 PreferredAlignment = DL->getStackAlignment(); |
| 611 |
| 612 LLVMContext &Ctx = M.getContext(); |
| 613 auto DISubprogramMap = makeSubprogramMap(M); |
| 614 |
| 615 // Change function signatures and fix a changed function body by |
| 616 // wiring the new arguments. Call sites are unchanged at this point. |
| 617 for (Module::iterator Iter = M.begin(), E = M.end(); Iter != E;) { |
| 618 Function *Func = Iter++; |
| 619 Changed |= simplifyFunction(Ctx, Func, DISubprogramMap); |
| 620 } |
| 621 |
| 622 // Fix call sites. |
| 623 for (auto &CallToFix : CallsToPatch) { |
| 624 fixCallSite(Ctx, CallToFix, PreferredAlignment); |
| 625 } |
| 626 |
| 627 for (auto &InvokeToFix : InvokesToPatch) { |
| 628 fixCallSite(Ctx, InvokeToFix, PreferredAlignment); |
| 629 } |
| 630 |
| 631 // Delete leftover functions - the ones with old signatures. |
| 632 for (auto &ToDelete : FunctionsToDelete) { |
| 633 ToDelete->eraseFromParent(); |
| 634 } |
| 635 |
| 636 return Changed; |
| 637 } |
| 638 |
| 639 ModulePass *llvm::createSimplifyStructRegSignaturesPass() { |
| 640 return new SimplifyStructRegSignatures(); |
| 641 } |
OLD | NEW |