llvm.org GIT mirror llvm / 4a544a7
Split the init.trampoline intrinsic, which currently combines GCC's init.trampoline and adjust.trampoline intrinsics, into two intrinsics like in GCC. While having one combined intrinsic is tempting, it is not natural because typically the trampoline initialization needs to be done in one function, and the result of adjust trampoline is needed in a different (nested) function. To get around this llvm-gcc hacks the nested function lowering code to insert an additional parent variable holding the adjust.trampoline result that can be accessed from the child function. Dragonegg doesn't have the luxury of tweaking GCC code, so it stored the result of adjust.trampoline in the memory GCC set aside for the trampoline itself (this is always available in the child function), and set up some new memory (using an alloca) to hold the trampoline. Unfortunately this breaks Go which allocates trampoline memory on the heap and wants to use it even after the parent has exited (!). Rather than doing even more hacks to get Go working, it seemed best to just use two intrinsics like in GCC. Patch mostly by Sanjoy Das. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@139140 91177308-0d34-0410-b5e6-96231b3b80d8 Duncan Sands 8 years ago
19 changed file(s) with 293 addition(s) and 85 deletion(s). Raw diff Collapse all Expand all
274274
275275
  • Debugger intrinsics
  • 276276
  • Exception Handling intrinsics
  • 277
  • Trampoline Intrinsic
  • 277
  • Trampoline Intrinsics
  • 278278
    279279
  • 'llvm.init.trampoline' Intrinsic
  • 280
  • 'llvm.adjust.trampoline' Intrinsic
  • 280281
    281282
    282283
  • Atomic intrinsics
  • 76797680
    76807681
    76817682

    7682 Trampoline Intrinsic
    7683 Trampoline Intrinsics
    76837684
    76847685
    76857686
    76867687
    7687

    This intrinsic makes it possible to excise one parameter, marked with

    7688

    These intrinsics make it possible to excise one parameter, marked with

    76887689 the nest attribute, from a function.
    76897690 The result is a callable
    76907691 function pointer lacking the nest parameter - the caller does not need to
    77017702
    
                      
                    
    77027703 %tramp = alloca [10 x i8], align 4 ; size and alignment only correct for X86
    77037704 %tramp1 = getelementptr [10 x i8]* %tramp, i32 0, i32 0
    7704 %p = call i8* @llvm.init.trampoline(i8* %tramp1, i8* bitcast (i32 (i8*, i32, i32)* @f to i8*), i8* %nval)
    7705 call i8* @llvm.init.trampoline(i8* %tramp1, i8* bitcast (i32 (i8*, i32, i32)* @f to i8*), i8* %nval)
    7706 %p = call i8* @llvm.adjust.trampoline(i8* %tramp1)
    77057707 %fp = bitcast i8* %p to i32 (i32, i32)*
    77067708
    77077709
    77197721
    77207722
    Syntax:
    77217723
    
                      
                    
    7722 declare i8* @llvm.init.trampoline(i8* <tramp>, i8* <func>, i8* <nval>)
    7723
    7724
    7725
    Overview:
    7726

    This fills the memory pointed to by tramp with code and returns a

    7727 function pointer suitable for executing it.

    7724 declare void @llvm.init.trampoline(i8* <tramp>, i8* <func>, i8* <nval>)
    7725
    7726
    7727
    Overview:
    7728

    This fills the memory pointed to by tramp with executable code,

    7729 turning it into a trampoline.

    77287730
    77297731
    Arguments:
    77307732

    The llvm.init.trampoline intrinsic takes three arguments, all

    77387740
    77397741
    Semantics:
    77407742

    The block of memory pointed to by tramp is filled with target

    7741 dependent code, turning it into a function. A pointer to this function is
    7742 returned, but needs to be bitcast to an appropriate
    7743 function pointer type before being called. The new function's signature
    7744 is the same as that of func with any arguments marked with
    7745 the nest attribute removed. At most one such nest argument
    7746 is allowed, and it must be of pointer type. Calling the new function is
    7747 equivalent to calling func with the same argument list, but
    7748 with nval used for the missing nest argument. If, after
    7749 calling llvm.init.trampoline, the memory pointed to
    7750 by tramp is modified, then the effect of any later call to the
    7751 returned function pointer is undefined.

    7743 dependent code, turning it into a function. Then tramp needs to be
    7744 passed to llvm.adjust.trampoline to get a pointer
    7745 which can be bitcast (to a new function) and
    7746 called. The new function's signature is the same as that of
    7747 func with any arguments marked with the nest attribute
    7748 removed. At most one such nest argument is allowed, and it must be of
    7749 pointer type. Calling the new function is equivalent to calling func
    7750 with the same argument list, but with nval used for the missing
    7751 nest argument. If, after calling llvm.init.trampoline, the
    7752 memory pointed to by tramp is modified, then the effect of any later call
    7753 to the returned function pointer is undefined.

    7754
    7755
    7756
    7757

    7758
    7759 'llvm.adjust.trampoline' Intrinsic
    7760
    7761
    7762
    7763
    7764
    7765
    Syntax:
    7766
    
                      
                    
    7767 declare i8* @llvm.adjust.trampoline(i8* <tramp>)
    7768
    7769
    7770
    Overview:
    7771

    This performs any required machine-specific adjustment to the address of a

    7772 trampoline (passed as tramp).

    7773
    7774
    Arguments:
    7775

    tramp must point to a block of memory which already has trampoline code

    7776 filled in by a previous call to llvm.init.trampoline
    7777 .

    7778
    7779
    Semantics:
    7780

    On some architectures the address of the code to be executed needs to be

    7781 different to the address where the trampoline is actually stored. This
    7782 intrinsic returns the executable address corresponding to tramp
    7783 after performing the required machine specific adjustments.
    7784 The pointer returned can then be bitcast and
    7785 executed.
    7786

    77527787
    77537788
    77547789
    565565 // HANDLENODE node - Used as a handle for various purposes.
    566566 HANDLENODE,
    567567
    568 // TRAMPOLINE - This corresponds to the init_trampoline intrinsic.
    569 // It takes as input a token chain, the pointer to the trampoline,
    570 // the pointer to the nested function, the pointer to pass for the
    571 // 'nest' parameter, a SRCVALUE for the trampoline and another for
    572 // the nested function (allowing targets to access the original
    573 // Function*). It produces the result of the intrinsic and a token
    574 // chain as output.
    575 TRAMPOLINE,
    568 // INIT_TRAMPOLINE - This corresponds to the init_trampoline intrinsic. It
    569 // takes as input a token chain, the pointer to the trampoline, the pointer
    570 // to the nested function, the pointer to pass for the 'nest' parameter, a
    571 // SRCVALUE for the trampoline and another for the nested function (allowing
    572 // targets to access the original Function*). It produces a token chain as
    573 // output.
    574 INIT_TRAMPOLINE,
    575
    576 // ADJUST_TRAMPOLINE - This corresponds to the adjust_trampoline intrinsic.
    577 // It takes a pointer to the trampoline and produces a (possibly) new
    578 // pointer to the same trampoline with platform-specific adjustments
    579 // applied. The pointer it returns points to an executable block of code.
    580 ADJUST_TRAMPOLINE,
    576581
    577582 // TRAP - Trapping instruction
    578583 TRAP,
    343343
    344344 //===------------------------ Trampoline Intrinsics -----------------------===//
    345345 //
    346 def int_init_trampoline : Intrinsic<[llvm_ptr_ty],
    346 def int_init_trampoline : Intrinsic<[],
    347347 [llvm_ptr_ty, llvm_ptr_ty, llvm_ptr_ty],
    348 [IntrReadWriteArgMem]>,
    349 GCCBuiltin<"__builtin_init_trampoline">;
    348 [IntrReadWriteArgMem, NoCapture<0>]>,
    349 GCCBuiltin<"__builtin_init_trampoline">;
    350
    351 def int_adjust_trampoline : Intrinsic<[llvm_ptr_ty], [llvm_ptr_ty],
    352 [IntrReadArgMem]>,
    353 GCCBuiltin<"__builtin_adjust_trampoline">;
    350354
    351355 //===------------------------ Overflow Intrinsics -------------------------===//
    352356 //
    871871 if (Action == TargetLowering::Legal)
    872872 Action = TargetLowering::Expand;
    873873 break;
    874 case ISD::TRAMPOLINE:
    874 case ISD::INIT_TRAMPOLINE:
    875 case ISD::ADJUST_TRAMPOLINE:
    875876 case ISD::FRAMEADDR:
    876877 case ISD::RETURNADDR:
    877878 // These operations lie about being legal: when they claim to be legal,
    60536053 case ISD::CTLZ: return "ctlz";
    60546054
    60556055 // Trampolines
    6056 case ISD::TRAMPOLINE: return "trampoline";
    6056 case ISD::INIT_TRAMPOLINE: return "init_trampoline";
    6057 case ISD::ADJUST_TRAMPOLINE: return "adjust_trampoline";
    60576058
    60586059 case ISD::CONDCODE:
    60596060 switch (cast(this)->get()) {
    50155015 Ops[4] = DAG.getSrcValue(I.getArgOperand(0));
    50165016 Ops[5] = DAG.getSrcValue(F);
    50175017
    5018 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
    5019 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
    5020 Ops, 6);
    5021
    5022 setValue(&I, Res);
    5023 DAG.setRoot(Res.getValue(1));
    5018 Res = DAG.getNode(ISD::INIT_TRAMPOLINE, dl, MVT::Other, Ops, 6);
    5019
    5020 DAG.setRoot(Res);
    5021 return 0;
    5022 }
    5023 case Intrinsic::adjust_trampoline: {
    5024 setValue(&I, DAG.getNode(ISD::ADJUST_TRAMPOLINE, dl,
    5025 TLI.getPointerTy(),
    5026 getValue(I.getArgOperand(0))));
    50245027 return 0;
    50255028 }
    50265029 case Intrinsic::gcroot:
    210210 setOperationAction(ISD::TRAP, MVT::Other, Legal);
    211211
    212212 // TRAMPOLINE is custom lowered.
    213 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
    213 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
    214 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
    214215
    215216 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
    216217 setOperationAction(ISD::VASTART , MVT::Other, Custom);
    13721373 return DAG.getLoad(VT, dl, InChain, Result, MachinePointerInfo(), false, false, 0);
    13731374 }
    13741375
    1375 SDValue PPCTargetLowering::LowerTRAMPOLINE(SDValue Op,
    1376 SelectionDAG &DAG) const {
    1376 SDValue PPCTargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
    1377 SelectionDAG &DAG) const {
    1378 return Op.getOperand(0);
    1379 }
    1380
    1381 SDValue PPCTargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
    1382 SelectionDAG &DAG) const {
    13771383 SDValue Chain = Op.getOperand(0);
    13781384 SDValue Trmp = Op.getOperand(1); // trampoline
    13791385 SDValue FPtr = Op.getOperand(2); // nested function
    14021408
    14031409 // Lower to a call to __trampoline_setup(Trmp, TrampSize, FPtr, ctx_reg)
    14041410 std::pair CallResult =
    1405 LowerCallTo(Chain, Op.getValueType().getTypeForEVT(*DAG.getContext()),
    1411 LowerCallTo(Chain, Type::getVoidTy(*DAG.getContext()),
    14061412 false, false, false, false, 0, CallingConv::C, false,
    14071413 /*isReturnValueUsed=*/true,
    14081414 DAG.getExternalSymbol("__trampoline_setup", PtrVT),
    14091415 Args, DAG, dl);
    14101416
    1411 SDValue Ops[] =
    1412 { CallResult.first, CallResult.second };
    1413
    1414 return DAG.getMergeValues(Ops, 2, dl);
    1417 return CallResult.second;
    14151418 }
    14161419
    14171420 SDValue PPCTargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG,
    44984501 case ISD::GlobalTLSAddress: llvm_unreachable("TLS not implemented for PPC");
    44994502 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
    45004503 case ISD::SETCC: return LowerSETCC(Op, DAG);
    4501 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
    4504 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
    4505 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
    45024506 case ISD::VASTART:
    45034507 return LowerVASTART(Op, DAG, PPCSubTarget);
    45044508
    389389 SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
    390390 SDValue LowerJumpTable(SDValue Op, SelectionDAG &DAG) const;
    391391 SDValue LowerSETCC(SDValue Op, SelectionDAG &DAG) const;
    392 SDValue LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    392 SDValue LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    393 SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    393394 SDValue LowerVASTART(SDValue Op, SelectionDAG &DAG,
    394395 const PPCSubtarget &Subtarget) const;
    395396 SDValue LowerVAARG(SDValue Op, SelectionDAG &DAG,
    503503 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
    504504 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
    505505
    506 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
    506 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
    507 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
    507508
    508509 setOperationAction(ISD::TRAP, MVT::Other, Legal);
    509510
    94059406 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
    94069407 }
    94079408
    9408 SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
    9409 SelectionDAG &DAG) const {
    9409 SDValue X86TargetLowering::LowerADJUST_TRAMPOLINE(SDValue Op,
    9410 SelectionDAG &DAG) const {
    9411 return Op.getOperand(0);
    9412 }
    9413
    9414 SDValue X86TargetLowering::LowerINIT_TRAMPOLINE(SDValue Op,
    9415 SelectionDAG &DAG) const {
    94109416 SDValue Root = Op.getOperand(0);
    94119417 SDValue Trmp = Op.getOperand(1); // trampoline
    94129418 SDValue FPtr = Op.getOperand(2); // nested function
    94709476 MachinePointerInfo(TrmpAddr, 22),
    94719477 false, false, 0);
    94729478
    9473 SDValue Ops[] =
    9474 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
    9475 return DAG.getMergeValues(Ops, 2, dl);
    9479 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6);
    94769480 } else {
    94779481 const Function *Func =
    94789482 cast(cast(Op.getOperand(5))->getValue());
    95529556 MachinePointerInfo(TrmpAddr, 6),
    95539557 false, false, 1);
    95549558
    9555 SDValue Ops[] =
    9556 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
    9557 return DAG.getMergeValues(Ops, 2, dl);
    9559 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4);
    95589560 }
    95599561 }
    95609562
    1035510357 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
    1035610358 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
    1035710359 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
    10358 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
    10360 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
    10361 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
    1035910362 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
    1036010363 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
    1036110364 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
    820820 SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
    821821 SDValue LowerFRAME_TO_ARGS_OFFSET(SDValue Op, SelectionDAG &DAG) const;
    822822 SDValue LowerEH_RETURN(SDValue Op, SelectionDAG &DAG) const;
    823 SDValue LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    823 SDValue LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    824 SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    824825 SDValue LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) const;
    825826 SDValue LowerCTLZ(SDValue Op, SelectionDAG &DAG) const;
    826827 SDValue LowerCTTZ(SDValue Op, SelectionDAG &DAG) const;
    146146 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
    147147
    148148 // TRAMPOLINE is custom lowered.
    149 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
    149 setOperationAction(ISD::INIT_TRAMPOLINE, MVT::Other, Custom);
    150 setOperationAction(ISD::ADJUST_TRAMPOLINE, MVT::Other, Custom);
    150151
    151152 maxStoresPerMemset = maxStoresPerMemsetOptSize = 4;
    152153 maxStoresPerMemmove = maxStoresPerMemmoveOptSize
    179180 case ISD::ADD:
    180181 case ISD::SUB: return ExpandADDSUB(Op.getNode(), DAG);
    181182 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
    182 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
    183 case ISD::INIT_TRAMPOLINE: return LowerINIT_TRAMPOLINE(Op, DAG);
    184 case ISD::ADJUST_TRAMPOLINE: return LowerADJUST_TRAMPOLINE(Op, DAG);
    183185 default:
    184186 llvm_unreachable("unimplemented operand");
    185187 return SDValue();
    788790 }
    789791
    790792 SDValue XCoreTargetLowering::
    791 LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
    793 LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
    794 return Op.getOperand(0);
    795 }
    796
    797 SDValue XCoreTargetLowering::
    798 LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const {
    792799 SDValue Chain = Op.getOperand(0);
    793800 SDValue Trmp = Op.getOperand(1); // trampoline
    794801 SDValue FPtr = Op.getOperand(2); // nested function
    840847 MachinePointerInfo(TrmpAddr, 16), false, false,
    841848 0);
    842849
    843 SDValue Ops[] =
    844 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 5) };
    845 return DAG.getMergeValues(Ops, 2, dl);
    850 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 5);
    846851 }
    847852
    848853 //===----------------------------------------------------------------------===//
    144144 SDValue LowerUMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
    145145 SDValue LowerSMUL_LOHI(SDValue Op, SelectionDAG &DAG) const;
    146146 SDValue LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) const;
    147 SDValue LowerTRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    147 SDValue LowerINIT_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    148 SDValue LowerADJUST_TRAMPOLINE(SDValue Op, SelectionDAG &DAG) const;
    148149
    149150 // Inline asm support
    150151 std::pair
    1010 #define INSTCOMBINE_INSTCOMBINE_H
    1111
    1212 #include "InstCombineWorklist.h"
    13 #include "llvm/IntrinsicInst.h"
    1314 #include "llvm/Operator.h"
    1415 #include "llvm/Pass.h"
    1516 #include "llvm/Analysis/ValueTracking.h"
    213214 Instruction *visitCallSite(CallSite CS);
    214215 Instruction *tryOptimizeCall(CallInst *CI, const TargetData *TD);
    215216 bool transformConstExprCastCall(CallSite CS);
    216 Instruction *transformCallThroughTrampoline(CallSite CS);
    217 Instruction *transformCallThroughTrampoline(CallSite CS,
    218 IntrinsicInst *Tramp);
    217219 Instruction *transformZExtICmp(ICmpInst *ICI, Instruction &CI,
    218220 bool DoXform = true);
    219221 Instruction *transformSExtICmp(ICmpInst *ICI, Instruction &CI);
    1111 //===----------------------------------------------------------------------===//
    1212
    1313 #include "InstCombine.h"
    14 #include "llvm/IntrinsicInst.h"
    1514 #include "llvm/Support/CallSite.h"
    1615 #include "llvm/Target/TargetData.h"
    1716 #include "llvm/Analysis/MemoryBuiltins.h"
    820819 return Simplifier.NewInstruction;
    821820 }
    822821
    822 static IntrinsicInst *FindInitTrampolineFromAlloca(Value *TrampMem) {
    823 // Strip off at most one level of pointer casts, looking for an alloca. This
    824 // is good enough in practice and simpler than handling any number of casts.
    825 Value *Underlying = TrampMem->stripPointerCasts();
    826 if (Underlying != TrampMem &&
    827 (!Underlying->hasOneUse() || *Underlying->use_begin() != TrampMem))
    828 return 0;
    829 if (!isa(Underlying))
    830 return 0;
    831
    832 IntrinsicInst *InitTrampoline = 0;
    833 for (Value::use_iterator I = TrampMem->use_begin(), E = TrampMem->use_end();
    834 I != E; I++) {
    835 IntrinsicInst *II = dyn_cast(*I);
    836 if (!II)
    837 return 0;
    838 if (II->getIntrinsicID() == Intrinsic::init_trampoline) {
    839 if (InitTrampoline)
    840 // More than one init_trampoline writes to this value. Give up.
    841 return 0;
    842 InitTrampoline = II;
    843 continue;
    844 }
    845 if (II->getIntrinsicID() == Intrinsic::adjust_trampoline)
    846 // Allow any number of calls to adjust.trampoline.
    847 continue;
    848 return 0;
    849 }
    850
    851 // No call to init.trampoline found.
    852 if (!InitTrampoline)
    853 return 0;
    854
    855 // Check that the alloca is being used in the expected way.
    856 if (InitTrampoline->getOperand(0) != TrampMem)
    857 return 0;
    858
    859 return InitTrampoline;
    860 }
    861
    862 static IntrinsicInst *FindInitTrampolineFromBB(IntrinsicInst *AdjustTramp,
    863 Value *TrampMem) {
    864 // Visit all the previous instructions in the basic block, and try to find a
    865 // init.trampoline which has a direct path to the adjust.trampoline.
    866 for (BasicBlock::iterator I = AdjustTramp,
    867 E = AdjustTramp->getParent()->begin(); I != E; ) {
    868 Instruction *Inst = --I;
    869 if (IntrinsicInst *II = dyn_cast(I))
    870 if (II->getIntrinsicID() == Intrinsic::init_trampoline &&
    871 II->getOperand(0) == TrampMem)
    872 return II;
    873 if (Inst->mayWriteToMemory())
    874 return 0;
    875 }
    876 return 0;
    877 }
    878
    879 // Given a call to llvm.adjust.trampoline, find and return the corresponding
    880 // call to llvm.init.trampoline if the call to the trampoline can be optimized
    881 // to a direct call to a function. Otherwise return NULL.
    882 //
    883 static IntrinsicInst *FindInitTrampoline(Value *Callee) {
    884 Callee = Callee->stripPointerCasts();
    885 IntrinsicInst *AdjustTramp = dyn_cast(Callee);
    886 if (!AdjustTramp ||
    887 AdjustTramp->getIntrinsicID() != Intrinsic::adjust_trampoline)
    888 return 0;
    889
    890 Value *TrampMem = AdjustTramp->getOperand(0);
    891
    892 if (IntrinsicInst *IT = FindInitTrampolineFromAlloca(TrampMem))
    893 return IT;
    894 if (IntrinsicInst *IT = FindInitTrampolineFromBB(AdjustTramp, TrampMem))
    895 return IT;
    896 return 0;
    897 }
    898
    823899 // visitCallSite - Improvements for call and invoke instructions.
    824900 //
    825901 Instruction *InstCombiner::visitCallSite(CallSite CS) {
    879955 return EraseInstFromFunction(*CS.getInstruction());
    880956 }
    881957
    882 if (BitCastInst *BC = dyn_cast(Callee))
    883 if (IntrinsicInst *In = dyn_cast(BC->getOperand(0)))
    884 if (In->getIntrinsicID() == Intrinsic::init_trampoline)
    885 return transformCallThroughTrampoline(CS);
    958 if (IntrinsicInst *II = FindInitTrampoline(Callee))
    959 return transformCallThroughTrampoline(CS, II);
    886960
    887961 PointerType *PTy = cast(Callee->getType());
    888962 FunctionType *FTy = cast(PTy->getElementType());
    11631237 return true;
    11641238 }
    11651239
    1166 // transformCallThroughTrampoline - Turn a call to a function created by the
    1167 // init_trampoline intrinsic into a direct call to the underlying function.
    1240 // transformCallThroughTrampoline - Turn a call to a function created by
    1241 // init_trampoline / adjust_trampoline intrinsic pair into a direct call to the
    1242 // underlying function.
    11681243 //
    1169 Instruction *InstCombiner::transformCallThroughTrampoline(CallSite CS) {
    1244 Instruction *
    1245 InstCombiner::transformCallThroughTrampoline(CallSite CS,
    1246 IntrinsicInst *Tramp) {
    11701247 Value *Callee = CS.getCalledValue();
    11711248 PointerType *PTy = cast(Callee->getType());
    11721249 FunctionType *FTy = cast(PTy->getElementType());
    11771254 if (Attrs.hasAttrSomewhere(Attribute::Nest))
    11781255 return 0;
    11791256
    1180 IntrinsicInst *Tramp =
    1181 cast(cast(Callee)->getOperand(0));
    1257 assert(Tramp &&
    1258 "transformCallThroughTrampoline called with incorrect CallSite.");
    11821259
    11831260 Function *NestF =cast(Tramp->getArgOperand(1)->stripPointerCasts());
    11841261 PointerType *NestFPTy = cast(NestF->getType());
    4242
    4343 switch (Name[0]) {
    4444 default: break;
    45 case 'i':
    46 // This upgrades the old llvm.init.trampoline to the new
    47 // llvm.init.trampoline and llvm.adjust.trampoline pair.
    48 if (Name == "init.trampoline") {
    49 // The new llvm.init.trampoline returns nothing.
    50 if (FTy->getReturnType()->isVoidTy())
    51 break;
    52
    53 assert(FTy->getNumParams() == 3 && "old init.trampoline takes 3 args!");
    54
    55 // Change the name of the old intrinsic so that we can play with its type.
    56 std::string NameTmp = F->getName();
    57 F->setName("");
    58 NewFn = cast(M->getOrInsertFunction(
    59 NameTmp,
    60 Type::getVoidTy(M->getContext()),
    61 FTy->getParamType(0), FTy->getParamType(1),
    62 FTy->getParamType(2), (Type *)0));
    63 return true;
    64 }
    4565 case 'p':
    4666 // This upgrades the llvm.prefetch intrinsic to accept one more parameter,
    4767 // which is a instruction / data cache identifier. The old version only
    215235 CI->eraseFromParent();
    216236 break;
    217237 }
    238 case Intrinsic::init_trampoline: {
    239
    240 // Transform
    241 // %tramp = call i8* llvm.init.trampoline (i8* x, i8* y, i8* z)
    242 // to
    243 // call void llvm.init.trampoline (i8* %x, i8* %y, i8* %z)
    244 // %tramp = call i8* llvm.adjust.trampoline (i8* %x)
    245
    246 Function *AdjustTrampolineFn =
    247 cast(Intrinsic::getDeclaration(F->getParent(),
    248 Intrinsic::adjust_trampoline));
    249
    250 IRBuilder<> Builder(C);
    251 Builder.SetInsertPoint(CI);
    252
    253 Builder.CreateCall3(NewFn, CI->getArgOperand(0), CI->getArgOperand(1),
    254 CI->getArgOperand(2));
    255
    256 CallInst *AdjustCall = Builder.CreateCall(AdjustTrampolineFn,
    257 CI->getArgOperand(0),
    258 CI->getName());
    259 if (!CI->use_empty())
    260 CI->replaceAllUsesWith(AdjustCall);
    261 CI->eraseFromParent();
    262 break;
    263 }
    218264 }
    219265 }
    220266
    3939 tail call void @llvm.prefetch(i8* %ptr, i32 0, i32 1)
    4040 ret void
    4141 }
    42
    43 declare i32 @nest_f(i8* nest, i32)
    44 declare i8* @llvm.init.trampoline(i8*, i8*, i8*)
    45
    46 define void @test_trampolines() {
    47 ; CHECK: call void @llvm.init.trampoline(i8* null, i8* bitcast (i32 (i8*, i32)* @nest_f to i8*), i8* null)
    48 ; CHECK: call i8* @llvm.adjust.trampoline(i8* null)
    49
    50 call i8* @llvm.init.trampoline(i8* null,
    51 i8* bitcast (i32 (i8*, i32)* @nest_f to i8*),
    52 i8* null)
    53 ret void
    54 }
    6666 store %struct.NSBitmapImageRep* %4, %struct.NSBitmapImageRep** %3, align 4
    6767 %TRAMP.91 = bitcast %struct.__builtin_trampoline* %TRAMP.9 to i8* ; [#uses=1]
    6868 %FRAME.72 = bitcast %"struct.FRAME.-[NSBitmapImageRep copyWithZone:]"* %FRAME.7 to i8* ; [#uses=1]
    69 %tramp = call i8* @llvm.init.trampoline(i8* %TRAMP.91, i8* bitcast (void (%"struct.FRAME.-[NSBitmapImageRep copyWithZone:]"*, %struct.__block_1*, %struct.CGImage*)* @__helper_1.1632 to i8*), i8* %FRAME.72) ; [#uses=1]
    69 call void @llvm.init.trampoline(i8* %TRAMP.91, i8* bitcast (void (%"struct.FRAME.-[NSBitmapImageRep copyWithZone:]"*, %struct.__block_1*, %struct.CGImage*)* @__helper_1.1632 to i8*), i8* %FRAME.72) ; [#uses=1]
    70 %tramp = call i8* @llvm.adjust.trampoline(i8* %TRAMP.91)
    7071 store i8* %tramp, i8** %0, align 4
    7172 %5 = getelementptr %"struct.FRAME.-[NSBitmapImageRep copyWithZone:]"* %FRAME.7, i32 0, i32 1 ; [#uses=1]
    7273 %6 = load i8** %0, align 4 ; [#uses=1]
    112113 ret %struct.objc_object* %retval5
    113114 }
    114115
    115 declare i8* @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    116 declare void @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    117 declare i8* @llvm.adjust.trampoline(i8*) nounwind
    116118
    117119 define internal void @__helper_1.1632(%"struct.FRAME.-[NSBitmapImageRep copyWithZone:]"* nest %CHAIN.8, %struct.__block_1* %_self, %struct.CGImage* %cgImage) nounwind {
    118120 entry:
    1010 %FRAME.0 = alloca %struct.FRAME.f, align 4
    1111 %TRAMP.23.sub = getelementptr inbounds [20 x i8]* %TRAMP.23, i32 0, i32 0
    1212 %FRAME.02 = bitcast %struct.FRAME.f* %FRAME.0 to i8*
    13 %tramp = call i8* @llvm.init.trampoline(i8* %TRAMP.23.sub, i8* bitcast (i32 (%struct.FRAME.f*)* @g.1101 to i8*), i8* %FRAME.02)
    13 call void @llvm.init.trampoline(i8* %TRAMP.23.sub, i8* bitcast (i32 (%struct.FRAME.f*)* @g.1101 to i8*), i8* %FRAME.02)
    14 %tramp = call i8* @llvm.adjust.trampoline(i8* %TRAMP.23.sub)
    1415 %0 = getelementptr inbounds %struct.FRAME.f* %FRAME.0, i32 0, i32 1
    1516 %1 = bitcast i8* %tramp to i32 ()*
    1617 store i32 ()* %1, i32 ()** %0, align 4
    3132 ret i32 %1
    3233 }
    3334
    34 declare i8* @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    35 declare void @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    36 declare i8* @llvm.adjust.trampoline(i8*) nounwind
    3537
    3638 declare void @h(i32 ()*)
    22 %struct.FRAME.nest = type { i32, i32 (...)* }
    33 %struct.__builtin_trampoline = type { [10 x i8] }
    44
    5 declare i8* @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    5 declare void @llvm.init.trampoline(i8*, i8*, i8*) nounwind
    6 declare i8* @llvm.adjust.trampoline(i8*) nounwind
    67
    78 declare i32 @f(%struct.FRAME.nest* nest , ...)
    89
    1415 %tmp3 = getelementptr %struct.FRAME.nest* %FRAME.0, i32 0, i32 0 ; [#uses=1]
    1516 store i32 %n, i32* %tmp3, align 8
    1617 %FRAME.06 = bitcast %struct.FRAME.nest* %FRAME.0 to i8* ; [#uses=1]
    17 %tramp = call i8* @llvm.init.trampoline( i8* %TRAMP.216.sub, i8* bitcast (i32 (%struct.FRAME.nest*, ...)* @f to i8*), i8* %FRAME.06 ) ; [#uses=1]
    18 call void @llvm.init.trampoline( i8* %TRAMP.216.sub, i8* bitcast (i32 (%struct.FRAME.nest*, ...)* @f to i8*), i8* %FRAME.06 ) ; [#uses=1]
    19 %tramp = call i8* @llvm.adjust.trampoline( i8* %TRAMP.216.sub)
    1820 %tmp7 = getelementptr %struct.FRAME.nest* %FRAME.0, i32 0, i32 1 ; [#uses=1]
    1921 %tmp89 = bitcast i8* %tramp to i32 (...)* ; [#uses=2]
    2022 store i32 (...)* %tmp89, i32 (...)** %tmp7, align 8