llvm.org GIT mirror llvm / f9be95f
add enough support for indirect branch for the feature test to pass (assembler,asmprinter, bc reader+writer) and document it. Codegen currently aborts on it. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@85274 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 10 years ago
17 changed file(s) with 483 addition(s) and 102 deletion(s). Raw diff Collapse all Expand all
109109
  • 'ret' Instruction
  • 110110
  • 'br' Instruction
  • 111111
  • 'switch' Instruction
  • 112
  • 'indbr' Instruction
  • 112113
  • 'invoke' Instruction
  • 113114
  • 'unwind' Instruction
  • 114115
  • 'unreachable' Instruction
  • 25102511 'ret' instruction, the
    25112512 'br' instruction, the
    25122513 'switch' instruction, the
    2514 ''indbr' Instruction, the
    25132515 'invoke' instruction, the
    25142516 'unwind' instruction, and the
    25152517 'unreachable' instruction.

    26672669
    26682670
    26692671
    2672
    2673
    2674
    2675
    2676 'indbr' Instruction
    2677
    2678
    2679
    2680
    2681
    Syntax:
    2682
    
                      
                    
    2683 indbr <somety>* <address>, [ label <dest1>, label <dest2>, ... ]
    2684
    2685
    2686
    Overview:
    2687
    2688

    The 'indbr' instruction implements an indirect branch to a label

    2689 within the current function, whose address is specified by
    2690 "address".

    2691
    2692
    Arguments:
    2693
    2694

    The 'address' argument is the address of the label to jump to. The

    2695 rest of the arguments indicate the full set of possible destinations that the
    2696 address may point to. Blocks are allowed to occur multiple times in the
    2697 destination list, though this isn't particularly useful.

    2698
    2699

    This destination list is required so that dataflow analysis has an accurate

    2700 understanding of the CFG.

    2701
    2702
    Semantics:
    2703
    2704

    Control transfers to the block specified in the address argument. All

    2705 possible destination blocks must be listed in the label list, otherwise this
    2706 instruction has undefined behavior. This implies that jumps to labels
    2707 defined in other functions have undefined behavior as well.

    2708
    2709
    Implementation:
    2710
    2711

    This is typically implemented with a jump through a register.

    2712
    2713
    Example:
    2714
    
                      
                    
    2715 switch i8* %Addr, [ label %bb1, label %bb2, label %bb3 ]
    2716
    2717
    2718
    2719
    26702720
    26712721
    26722722
    208208
    209209 FUNC_CODE_INST_RET = 10, // RET: [opty,opval]
    210210 FUNC_CODE_INST_BR = 11, // BR: [bb#, bb#, cond] or [bb#]
    211 FUNC_CODE_INST_SWITCH = 12, // SWITCH: [opty, opval, n, n x ops]
    211 FUNC_CODE_INST_SWITCH = 12, // SWITCH: [opty, operands...]
    212212 FUNC_CODE_INST_INVOKE = 13, // INVOKE: [attr, fnty, op0,op1, ...]
    213213 FUNC_CODE_INST_UNWIND = 14, // UNWIND
    214214 FUNC_CODE_INST_UNREACHABLE = 15, // UNREACHABLE
    235235 FUNC_CODE_INST_CMP2 = 28, // CMP2: [opty, opval, opval, pred]
    236236 // new select on i1 or [N x i1]
    237237 FUNC_CODE_INST_VSELECT = 29, // VSELECT: [ty,opval,opval,predty,pred]
    238 FUNC_CODE_INST_INBOUNDS_GEP = 30 // INBOUNDS_GEP: [n x operands]
    238 FUNC_CODE_INST_INBOUNDS_GEP= 30, // INBOUNDS_GEP: [n x operands]
    239 FUNC_CODE_INST_INDBR = 31 // INDBR: [opty, operands...]
    239240 };
    240241 } // End bitc namespace
    241242 } // End llvm namespace
    9696 HANDLE_TERM_INST ( 1, Ret , ReturnInst)
    9797 HANDLE_TERM_INST ( 2, Br , BranchInst)
    9898 HANDLE_TERM_INST ( 3, Switch , SwitchInst)
    99 HANDLE_TERM_INST ( 4, Invoke , InvokeInst)
    100 HANDLE_TERM_INST ( 5, Unwind , UnwindInst)
    101 HANDLE_TERM_INST ( 6, Unreachable, UnreachableInst)
    102 LAST_TERM_INST ( 6)
    99 HANDLE_TERM_INST ( 4, IndBr , IndBrInst)
    100 HANDLE_TERM_INST ( 5, Invoke , InvokeInst)
    101 HANDLE_TERM_INST ( 6, Unwind , UnwindInst)
    102 HANDLE_TERM_INST ( 7, Unreachable, UnreachableInst)
    103 LAST_TERM_INST ( 7)
    103104
    104105 // Standard binary operators...
    105 FIRST_BINARY_INST( 7)
    106 HANDLE_BINARY_INST( 7, Add , BinaryOperator)
    107 HANDLE_BINARY_INST( 8, FAdd , BinaryOperator)
    108 HANDLE_BINARY_INST( 9, Sub , BinaryOperator)
    109 HANDLE_BINARY_INST(10, FSub , BinaryOperator)
    110 HANDLE_BINARY_INST(11, Mul , BinaryOperator)
    111 HANDLE_BINARY_INST(12, FMul , BinaryOperator)
    112 HANDLE_BINARY_INST(13, UDiv , BinaryOperator)
    113 HANDLE_BINARY_INST(14, SDiv , BinaryOperator)
    114 HANDLE_BINARY_INST(15, FDiv , BinaryOperator)
    115 HANDLE_BINARY_INST(16, URem , BinaryOperator)
    116 HANDLE_BINARY_INST(17, SRem , BinaryOperator)
    117 HANDLE_BINARY_INST(18, FRem , BinaryOperator)
    106 FIRST_BINARY_INST( 8)
    107 HANDLE_BINARY_INST( 8, Add , BinaryOperator)
    108 HANDLE_BINARY_INST( 9, FAdd , BinaryOperator)
    109 HANDLE_BINARY_INST(10, Sub , BinaryOperator)
    110 HANDLE_BINARY_INST(11, FSub , BinaryOperator)
    111 HANDLE_BINARY_INST(12, Mul , BinaryOperator)
    112 HANDLE_BINARY_INST(13, FMul , BinaryOperator)
    113 HANDLE_BINARY_INST(14, UDiv , BinaryOperator)
    114 HANDLE_BINARY_INST(15, SDiv , BinaryOperator)
    115 HANDLE_BINARY_INST(16, FDiv , BinaryOperator)
    116 HANDLE_BINARY_INST(17, URem , BinaryOperator)
    117 HANDLE_BINARY_INST(18, SRem , BinaryOperator)
    118 HANDLE_BINARY_INST(19, FRem , BinaryOperator)
    118119
    119120 // Logical operators (integer operands)
    120 HANDLE_BINARY_INST(19, Shl , BinaryOperator) // Shift left (logical)
    121 HANDLE_BINARY_INST(20, LShr , BinaryOperator) // Shift right (logical)
    122 HANDLE_BINARY_INST(21, AShr , BinaryOperator) // Shift right (arithmetic)
    123 HANDLE_BINARY_INST(22, And , BinaryOperator)
    124 HANDLE_BINARY_INST(23, Or , BinaryOperator)
    125 HANDLE_BINARY_INST(24, Xor , BinaryOperator)
    126 LAST_BINARY_INST(24)
    121 HANDLE_BINARY_INST(20, Shl , BinaryOperator) // Shift left (logical)
    122 HANDLE_BINARY_INST(21, LShr , BinaryOperator) // Shift right (logical)
    123 HANDLE_BINARY_INST(22, AShr , BinaryOperator) // Shift right (arithmetic)
    124 HANDLE_BINARY_INST(23, And , BinaryOperator)
    125 HANDLE_BINARY_INST(24, Or , BinaryOperator)
    126 HANDLE_BINARY_INST(25, Xor , BinaryOperator)
    127 LAST_BINARY_INST(25)
    127128
    128129 // Memory operators...
    129 FIRST_MEMORY_INST(25)
    130 HANDLE_MEMORY_INST(25, Alloca, AllocaInst) // Stack management
    131 HANDLE_MEMORY_INST(26, Load , LoadInst ) // Memory manipulation instrs
    132 HANDLE_MEMORY_INST(27, Store , StoreInst )
    133 HANDLE_MEMORY_INST(28, GetElementPtr, GetElementPtrInst)
    134 LAST_MEMORY_INST(28)
    130 FIRST_MEMORY_INST(26)
    131 HANDLE_MEMORY_INST(26, Alloca, AllocaInst) // Stack management
    132 HANDLE_MEMORY_INST(27, Load , LoadInst ) // Memory manipulation instrs
    133 HANDLE_MEMORY_INST(28, Store , StoreInst )
    134 HANDLE_MEMORY_INST(29, GetElementPtr, GetElementPtrInst)
    135 LAST_MEMORY_INST(29)
    135136
    136137 // Cast operators ...
    137138 // NOTE: The order matters here because CastInst::isEliminableCastPair
    138139 // NOTE: (see Instructions.cpp) encodes a table based on this ordering.
    139 FIRST_CAST_INST(29)
    140 HANDLE_CAST_INST(29, Trunc , TruncInst ) // Truncate integers
    141 HANDLE_CAST_INST(30, ZExt , ZExtInst ) // Zero extend integers
    142 HANDLE_CAST_INST(31, SExt , SExtInst ) // Sign extend integers
    143 HANDLE_CAST_INST(32, FPToUI , FPToUIInst ) // floating point -> UInt
    144 HANDLE_CAST_INST(33, FPToSI , FPToSIInst ) // floating point -> SInt
    145 HANDLE_CAST_INST(34, UIToFP , UIToFPInst ) // UInt -> floating point
    146 HANDLE_CAST_INST(35, SIToFP , SIToFPInst ) // SInt -> floating point
    147 HANDLE_CAST_INST(36, FPTrunc , FPTruncInst ) // Truncate floating point
    148 HANDLE_CAST_INST(37, FPExt , FPExtInst ) // Extend floating point
    149 HANDLE_CAST_INST(38, PtrToInt, PtrToIntInst) // Pointer -> Integer
    150 HANDLE_CAST_INST(39, IntToPtr, IntToPtrInst) // Integer -> Pointer
    151 HANDLE_CAST_INST(40, BitCast , BitCastInst ) // Type cast
    152 LAST_CAST_INST(40)
    140 FIRST_CAST_INST(30)
    141 HANDLE_CAST_INST(30, Trunc , TruncInst ) // Truncate integers
    142 HANDLE_CAST_INST(31, ZExt , ZExtInst ) // Zero extend integers
    143 HANDLE_CAST_INST(32, SExt , SExtInst ) // Sign extend integers
    144 HANDLE_CAST_INST(33, FPToUI , FPToUIInst ) // floating point -> UInt
    145 HANDLE_CAST_INST(34, FPToSI , FPToSIInst ) // floating point -> SInt
    146 HANDLE_CAST_INST(35, UIToFP , UIToFPInst ) // UInt -> floating point
    147 HANDLE_CAST_INST(36, SIToFP , SIToFPInst ) // SInt -> floating point
    148 HANDLE_CAST_INST(37, FPTrunc , FPTruncInst ) // Truncate floating point
    149 HANDLE_CAST_INST(38, FPExt , FPExtInst ) // Extend floating point
    150 HANDLE_CAST_INST(39, PtrToInt, PtrToIntInst) // Pointer -> Integer
    151 HANDLE_CAST_INST(40, IntToPtr, IntToPtrInst) // Integer -> Pointer
    152 HANDLE_CAST_INST(41, BitCast , BitCastInst ) // Type cast
    153 LAST_CAST_INST(41)
    153154
    154155 // Other operators...
    155 FIRST_OTHER_INST(41)
    156 HANDLE_OTHER_INST(41, ICmp , ICmpInst ) // Integer comparison instruction
    157 HANDLE_OTHER_INST(42, FCmp , FCmpInst ) // Floating point comparison instr.
    158 HANDLE_OTHER_INST(43, PHI , PHINode ) // PHI node instruction
    159 HANDLE_OTHER_INST(44, Call , CallInst ) // Call a function
    160 HANDLE_OTHER_INST(45, Select , SelectInst ) // select instruction
    161 HANDLE_OTHER_INST(46, UserOp1, Instruction) // May be used internally in a pass
    162 HANDLE_OTHER_INST(47, UserOp2, Instruction) // Internal to passes only
    163 HANDLE_OTHER_INST(48, VAArg , VAArgInst ) // vaarg instruction
    164 HANDLE_OTHER_INST(49, ExtractElement, ExtractElementInst)// extract from vector
    165 HANDLE_OTHER_INST(50, InsertElement, InsertElementInst) // insert into vector
    166 HANDLE_OTHER_INST(51, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
    167 HANDLE_OTHER_INST(52, ExtractValue, ExtractValueInst)// extract from aggregate
    168 HANDLE_OTHER_INST(53, InsertValue, InsertValueInst) // insert into aggregate
    156 FIRST_OTHER_INST(42)
    157 HANDLE_OTHER_INST(42, ICmp , ICmpInst ) // Integer comparison instruction
    158 HANDLE_OTHER_INST(43, FCmp , FCmpInst ) // Floating point comparison instr.
    159 HANDLE_OTHER_INST(44, PHI , PHINode ) // PHI node instruction
    160 HANDLE_OTHER_INST(45, Call , CallInst ) // Call a function
    161 HANDLE_OTHER_INST(46, Select , SelectInst ) // select instruction
    162 HANDLE_OTHER_INST(47, UserOp1, Instruction) // May be used internally in a pass
    163 HANDLE_OTHER_INST(48, UserOp2, Instruction) // Internal to passes only
    164 HANDLE_OTHER_INST(49, VAArg , VAArgInst ) // vaarg instruction
    165 HANDLE_OTHER_INST(50, ExtractElement, ExtractElementInst)// extract from vector
    166 HANDLE_OTHER_INST(51, InsertElement, InsertElementInst) // insert into vector
    167 HANDLE_OTHER_INST(52, ShuffleVector, ShuffleVectorInst) // shuffle two vectors.
    168 HANDLE_OTHER_INST(53, ExtractValue, ExtractValueInst)// extract from aggregate
    169 HANDLE_OTHER_INST(54, InsertValue, InsertValueInst) // insert into aggregate
    169170
    170 LAST_OTHER_INST(53)
    171 LAST_OTHER_INST(54)
    171172
    172173 #undef FIRST_TERM_INST
    173174 #undef HANDLE_TERM_INST
    20752075 // Operand[1] = Default basic block destination
    20762076 // Operand[2n ] = Value to match
    20772077 // Operand[2n+1] = BasicBlock to go to on match
    2078 SwitchInst(const SwitchInst &RI);
    2078 SwitchInst(const SwitchInst &SI);
    20792079 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
    20802080 void resizeOperands(unsigned No);
    20812081 // allocate space for exactly zero operands
    20872087 /// be specified here to make memory allocation more efficient. This
    20882088 /// constructor can also autoinsert before another instruction.
    20892089 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
    2090 Instruction *InsertBefore = 0);
    2090 Instruction *InsertBefore);
    20912091
    20922092 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
    20932093 /// switch on and a default destination. The number of additional cases can
    22122212 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
    22132213
    22142214
    2215 //===----------------------------------------------------------------------===//
    2216 // IndBrInst Class
    2217 //===----------------------------------------------------------------------===//
    2218
    2219 //===---------------------------------------------------------------------------
    2220 /// IndBrInst - Indirect Branch Instruction.
    2221 ///
    2222 class IndBrInst : public TerminatorInst {
    2223 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
    2224 unsigned ReservedSpace;
    2225 // Operand[0] = Value to switch on
    2226 // Operand[1] = Default basic block destination
    2227 // Operand[2n ] = Value to match
    2228 // Operand[2n+1] = BasicBlock to go to on match
    2229 IndBrInst(const IndBrInst &IBI);
    2230 void init(Value *Address, unsigned NumDests);
    2231 void resizeOperands(unsigned No);
    2232 // allocate space for exactly zero operands
    2233 void *operator new(size_t s) {
    2234 return User::operator new(s, 0);
    2235 }
    2236 /// IndBrInst ctor - Create a new indbr instruction, specifying an Address to
    2237 /// jump to. The number of expected destinations can be specified here to
    2238 /// make memory allocation more efficient. This constructor can also
    2239 /// autoinsert before another instruction.
    2240 IndBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
    2241
    2242 /// IndBrInst ctor - Create a new indbr instruction, specifying an Address to
    2243 /// jump to. The number of expected destinations can be specified here to
    2244 /// make memory allocation more efficient. This constructor also autoinserts
    2245 /// at the end of the specified BasicBlock.
    2246 IndBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
    2247 public:
    2248 static IndBrInst *Create(Value *Address, unsigned NumDests,
    2249 Instruction *InsertBefore = 0) {
    2250 return new IndBrInst(Address, NumDests, InsertBefore);
    2251 }
    2252 static IndBrInst *Create(Value *Address, unsigned NumDests,
    2253 BasicBlock *InsertAtEnd) {
    2254 return new IndBrInst(Address, NumDests, InsertAtEnd);
    2255 }
    2256 ~IndBrInst();
    2257
    2258 /// Provide fast operand accessors.
    2259 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
    2260
    2261 // Accessor Methods for IndBr instruction.
    2262 Value *getAddress() { return getOperand(0); }
    2263 const Value *getAddress() const { return getOperand(0); }
    2264 void setAddress(Value *V) { setOperand(0, V); }
    2265
    2266
    2267 /// getNumDestinations - return the number of possible destinations in this
    2268 /// indbr instruction.
    2269 unsigned getNumDestinations() const { return getNumOperands()-1; }
    2270
    2271 /// getDestination - Return the specified destination.
    2272 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
    2273 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
    2274
    2275 /// addDestination - Add a destination.
    2276 ///
    2277 void addDestination(BasicBlock *Dest);
    2278
    2279 /// removeDestination - This method removes the specified successor from the
    2280 /// indbr instruction.
    2281 void removeDestination(unsigned i);
    2282
    2283 virtual IndBrInst *clone() const;
    2284
    2285 unsigned getNumSuccessors() const { return getNumOperands()-1; }
    2286 BasicBlock *getSuccessor(unsigned i) const {
    2287 return cast(getOperand(i+1));
    2288 }
    2289 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
    2290 setOperand(i+1, (Value*)NewSucc);
    2291 }
    2292
    2293 // Methods for support type inquiry through isa, cast, and dyn_cast:
    2294 static inline bool classof(const IndBrInst *) { return true; }
    2295 static inline bool classof(const Instruction *I) {
    2296 return I->getOpcode() == Instruction::IndBr;
    2297 }
    2298 static inline bool classof(const Value *V) {
    2299 return isa(V) && classof(cast(V));
    2300 }
    2301 private:
    2302 virtual BasicBlock *getSuccessorV(unsigned idx) const;
    2303 virtual unsigned getNumSuccessorsV() const;
    2304 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
    2305 };
    2306
    2307 template <>
    2308 struct OperandTraits : public HungoffOperandTraits<1> {
    2309 };
    2310
    2311 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndBrInst, Value)
    2312
    2313
    22152314 //===----------------------------------------------------------------------===//
    22162315 // InvokeInst Class
    22172316 //===----------------------------------------------------------------------===//
    159159 RetTy visitReturnInst(ReturnInst &I) { DELEGATE(TerminatorInst);}
    160160 RetTy visitBranchInst(BranchInst &I) { DELEGATE(TerminatorInst);}
    161161 RetTy visitSwitchInst(SwitchInst &I) { DELEGATE(TerminatorInst);}
    162 RetTy visitIndBrInst(IndBrInst &I) { DELEGATE(TerminatorInst);}
    162163 RetTy visitInvokeInst(InvokeInst &I) { DELEGATE(TerminatorInst);}
    163164 RetTy visitUnwindInst(UnwindInst &I) { DELEGATE(TerminatorInst);}
    164165 RetTy visitUnreachableInst(UnreachableInst &I) { DELEGATE(TerminatorInst);}
    644644 INSTKEYWORD(ret, Ret);
    645645 INSTKEYWORD(br, Br);
    646646 INSTKEYWORD(switch, Switch);
    647 INSTKEYWORD(indbr, IndBr);
    647648 INSTKEYWORD(invoke, Invoke);
    648649 INSTKEYWORD(unwind, Unwind);
    649650 INSTKEYWORD(unreachable, Unreachable);
    24112411 ParseValue(T, V, PFS);
    24122412 }
    24132413
    2414 bool LLParser::ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
    2415 PerFunctionState &PFS) {
    2416 Value *V;
    2417 Loc = Lex.getLoc();
    2418 if (ParseTypeAndValue(V, PFS)) return true;
    2419 if (!isa(V))
    2420 return Error(Loc, "expected a basic block");
    2421 BB = cast(V);
    2422 return false;
    2423 }
    2424
    2425
    24142426 /// FunctionHeader
    24152427 /// ::= OptionalLinkage OptionalVisibility OptionalCallingConv OptRetAttrs
    24162428 /// Type GlobalName '(' ArgList ')' OptFuncAttrs OptSection
    27182730 case lltok::kw_ret: return ParseRet(Inst, BB, PFS);
    27192731 case lltok::kw_br: return ParseBr(Inst, PFS);
    27202732 case lltok::kw_switch: return ParseSwitch(Inst, PFS);
    2733 case lltok::kw_indbr: return ParseIndBr(Inst, PFS);
    27212734 case lltok::kw_invoke: return ParseInvoke(Inst, PFS);
    27222735 // Binary Operators.
    27232736 case lltok::kw_add:
    29212934 /// ::= 'br' TypeAndValue ',' TypeAndValue ',' TypeAndValue
    29222935 bool LLParser::ParseBr(Instruction *&Inst, PerFunctionState &PFS) {
    29232936 LocTy Loc, Loc2;
    2924 Value *Op0, *Op1, *Op2;
    2937 Value *Op0;
    2938 BasicBlock *Op1, *Op2;
    29252939 if (ParseTypeAndValue(Op0, Loc, PFS)) return true;
    29262940
    29272941 if (BasicBlock *BB = dyn_cast(Op0)) {
    29332947 return Error(Loc, "branch condition must have 'i1' type");
    29342948
    29352949 if (ParseToken(lltok::comma, "expected ',' after branch condition") ||
    2936 ParseTypeAndValue(Op1, Loc, PFS) ||
    2950 ParseTypeAndBasicBlock(Op1, Loc, PFS) ||
    29372951 ParseToken(lltok::comma, "expected ',' after true destination") ||
    2938 ParseTypeAndValue(Op2, Loc2, PFS))
    2952 ParseTypeAndBasicBlock(Op2, Loc2, PFS))
    29392953 return true;
    29402954
    2941 if (!isa(Op1))
    2942 return Error(Loc, "true destination of branch must be a basic block");
    2943 if (!isa(Op2))
    2944 return Error(Loc2, "true destination of branch must be a basic block");
    2945
    2946 Inst = BranchInst::Create(cast(Op1), cast(Op2), Op0);
    2955 Inst = BranchInst::Create(Op1, Op2, Op0);
    29472956 return false;
    29482957 }
    29492958
    29542963 /// ::= (TypeAndValue ',' TypeAndValue)*
    29552964 bool LLParser::ParseSwitch(Instruction *&Inst, PerFunctionState &PFS) {
    29562965 LocTy CondLoc, BBLoc;
    2957 Value *Cond, *DefaultBB;
    2966 Value *Cond;
    2967 BasicBlock *DefaultBB;
    29582968 if (ParseTypeAndValue(Cond, CondLoc, PFS) ||
    29592969 ParseToken(lltok::comma, "expected ',' after switch condition") ||
    2960 ParseTypeAndValue(DefaultBB, BBLoc, PFS) ||
    2970 ParseTypeAndBasicBlock(DefaultBB, BBLoc, PFS) ||
    29612971 ParseToken(lltok::lsquare, "expected '[' with switch table"))
    29622972 return true;
    29632973
    29642974 if (!isa(Cond->getType()))
    29652975 return Error(CondLoc, "switch condition must have integer type");
    2966 if (!isa(DefaultBB))
    2967 return Error(BBLoc, "default destination must be a basic block");
    29682976
    29692977 // Parse the jump table pairs.
    29702978 SmallPtrSet SeenCases;
    29712979 SmallVector, 32> Table;
    29722980 while (Lex.getKind() != lltok::rsquare) {
    2973 Value *Constant, *DestBB;
    2981 Value *Constant;
    2982 BasicBlock *DestBB;
    29742983
    29752984 if (ParseTypeAndValue(Constant, CondLoc, PFS) ||
    29762985 ParseToken(lltok::comma, "expected ',' after case value") ||
    2977 ParseTypeAndValue(DestBB, BBLoc, PFS))
    2986 ParseTypeAndBasicBlock(DestBB, PFS))
    29782987 return true;
    2979
    2988
    29802989 if (!SeenCases.insert(Constant))
    29812990 return Error(CondLoc, "duplicate case value in switch");
    29822991 if (!isa(Constant))
    29832992 return Error(CondLoc, "case value is not a constant integer");
    2984 if (!isa(DestBB))
    2985 return Error(BBLoc, "case destination is not a basic block");
    2986
    2987 Table.push_back(std::make_pair(cast(Constant),
    2988 cast(DestBB)));
    2993
    2994 Table.push_back(std::make_pair(cast(Constant), DestBB));
    29892995 }
    29902996
    29912997 Lex.Lex(); // Eat the ']'.
    29922998
    2993 SwitchInst *SI = SwitchInst::Create(Cond, cast(DefaultBB),
    2994 Table.size());
    2999 SwitchInst *SI = SwitchInst::Create(Cond, DefaultBB, Table.size());
    29953000 for (unsigned i = 0, e = Table.size(); i != e; ++i)
    29963001 SI->addCase(Table[i].first, Table[i].second);
    29973002 Inst = SI;
    29983003 return false;
    29993004 }
    3005
    3006 /// ParseIndBr
    3007 /// Instruction
    3008 /// ::= 'indbr' TypeAndValue ',' '[' LabelList ']'
    3009 bool LLParser::ParseIndBr(Instruction *&Inst, PerFunctionState &PFS) {
    3010 LocTy AddrLoc;
    3011 Value *Address;
    3012 if (ParseTypeAndValue(Address, AddrLoc, PFS) ||
    3013 ParseToken(lltok::comma, "expected ',' after indbr address") ||
    3014 ParseToken(lltok::lsquare, "expected '[' with indbr"))
    3015 return true;
    3016
    3017 if (!isa(Address->getType()))
    3018 return Error(AddrLoc, "indbr address must have pointer type");
    3019
    3020 // Parse the destination list.
    3021 SmallVector DestList;
    3022
    3023 if (Lex.getKind() != lltok::rsquare) {
    3024 BasicBlock *DestBB;
    3025 if (ParseTypeAndBasicBlock(DestBB, PFS))
    3026 return true;
    3027 DestList.push_back(DestBB);
    3028
    3029 while (EatIfPresent(lltok::comma)) {
    3030 if (ParseTypeAndBasicBlock(DestBB, PFS))
    3031 return true;
    3032 DestList.push_back(DestBB);
    3033 }
    3034 }
    3035
    3036 if (ParseToken(lltok::rsquare, "expected ']' at end of block list"))
    3037 return true;
    3038
    3039 IndBrInst *IBI = IndBrInst::Create(Address, DestList.size());
    3040 for (unsigned i = 0, e = DestList.size(); i != e; ++i)
    3041 IBI->addDestination(DestList[i]);
    3042 Inst = IBI;
    3043 return false;
    3044 }
    3045
    30003046
    30013047 /// ParseInvoke
    30023048 /// ::= 'invoke' OptionalCallingConv OptionalAttrs Type Value ParamList
    30103056 ValID CalleeID;
    30113057 SmallVector ArgList;
    30123058
    3013 Value *NormalBB, *UnwindBB;
    3059 BasicBlock *NormalBB, *UnwindBB;
    30143060 if (ParseOptionalCallingConv(CC) ||
    30153061 ParseOptionalAttrs(RetAttrs, 1) ||
    30163062 ParseType(RetType, RetTypeLoc, true /*void allowed*/) ||
    30183064 ParseParameterList(ArgList, PFS) ||
    30193065 ParseOptionalAttrs(FnAttrs, 2) ||
    30203066 ParseToken(lltok::kw_to, "expected 'to' in invoke") ||
    3021 ParseTypeAndValue(NormalBB, PFS) ||
    3067 ParseTypeAndBasicBlock(NormalBB, PFS) ||
    30223068 ParseToken(lltok::kw_unwind, "expected 'unwind' in invoke") ||
    3023 ParseTypeAndValue(UnwindBB, PFS))
    3069 ParseTypeAndBasicBlock(UnwindBB, PFS))
    30243070 return true;
    3025
    3026 if (!isa(NormalBB))
    3027 return Error(CallLoc, "normal destination is not a basic block");
    3028 if (!isa(UnwindBB))
    3029 return Error(CallLoc, "unwind destination is not a basic block");
    30303071
    30313072 // If RetType is a non-function pointer type, then this is the short syntax
    30323073 // for the call, which means that RetType is just the return type. Infer the
    30953136 // Finish off the Attributes and check them
    30963137 AttrListPtr PAL = AttrListPtr::get(Attrs.begin(), Attrs.end());
    30973138
    3098 InvokeInst *II = InvokeInst::Create(Callee, cast(NormalBB),
    3099 cast(UnwindBB),
    3139 InvokeInst *II = InvokeInst::Create(Callee, NormalBB, UnwindBB,
    31003140 Args.begin(), Args.end());
    31013141 II->setCallingConv(CC);
    31023142 II->setAttributes(PAL);
    229229 Loc = Lex.getLoc();
    230230 return ParseTypeAndValue(V, PFS);
    231231 }
    232
    232 bool ParseTypeAndBasicBlock(BasicBlock *&BB, LocTy &Loc,
    233 PerFunctionState &PFS);
    234 bool ParseTypeAndBasicBlock(BasicBlock *&BB, PerFunctionState &PFS) {
    235 LocTy Loc;
    236 return ParseTypeAndBasicBlock(BB, Loc, PFS);
    237 }
    238
    233239 struct ParamInfo {
    234240 LocTy Loc;
    235241 Value *V;
    263269 bool ParseRet(Instruction *&Inst, BasicBlock *BB, PerFunctionState &PFS);
    264270 bool ParseBr(Instruction *&Inst, PerFunctionState &PFS);
    265271 bool ParseSwitch(Instruction *&Inst, PerFunctionState &PFS);
    272 bool ParseIndBr(Instruction *&Inst, PerFunctionState &PFS);
    266273 bool ParseInvoke(Instruction *&Inst, PerFunctionState &PFS);
    267274
    268275 bool ParseArithmetic(Instruction *&I, PerFunctionState &PFS, unsigned Opc,
    110110 kw_fptoui, kw_fptosi, kw_inttoptr, kw_ptrtoint, kw_bitcast,
    111111 kw_select, kw_va_arg,
    112112
    113 kw_ret, kw_br, kw_switch, kw_invoke, kw_unwind, kw_unreachable,
    113 kw_ret, kw_br, kw_switch, kw_indbr, kw_invoke, kw_unwind, kw_unreachable,
    114114
    115115 kw_malloc, kw_alloca, kw_free, kw_load, kw_store, kw_getelementptr,
    116116
    19501950 }
    19511951 break;
    19521952 }
    1953 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, opval, n, n x ops]
    1953 case bitc::FUNC_CODE_INST_SWITCH: { // SWITCH: [opty, op0, op1, ...]
    19541954 if (Record.size() < 3 || (Record.size() & 1) == 0)
    19551955 return Error("Invalid SWITCH record");
    19561956 const Type *OpTy = getTypeByID(Record[0]);
    19741974 I = SI;
    19751975 break;
    19761976 }
    1977
    1977 case bitc::FUNC_CODE_INST_INDBR: { // INDBR: [opty, op0, op1, ...]
    1978 if (Record.size() < 2)
    1979 return Error("Invalid INDBR record");
    1980 const Type *OpTy = getTypeByID(Record[0]);
    1981 Value *Address = getFnValueByID(Record[1], OpTy);
    1982 if (OpTy == 0 || Address == 0)
    1983 return Error("Invalid INDBR record");
    1984 unsigned NumDests = Record.size()-2;
    1985 IndBrInst *IBI = IndBrInst::Create(Address, NumDests);
    1986 InstructionList.push_back(IBI);
    1987 for (unsigned i = 0, e = NumDests; i != e; ++i) {
    1988 if (BasicBlock *DestBB = getBasicBlock(Record[2+i])) {
    1989 IBI->addDestination(DestBB);
    1990 } else {
    1991 delete IBI;
    1992 return Error("Invalid INDBR record!");
    1993 }
    1994 }
    1995 I = IBI;
    1996 break;
    1997 }
    1998
    19781999 case bitc::FUNC_CODE_INST_INVOKE: {
    19792000 // INVOKE: [attrs, cc, normBB, unwindBB, fnty, op0,op1,op2, ...]
    19802001 if (Record.size() < 4) return Error("Invalid INVOKE record");
    10001000 case Instruction::Br:
    10011001 {
    10021002 Code = bitc::FUNC_CODE_INST_BR;
    1003 BranchInst &II(cast(I));
    1003 BranchInst &II = cast(I);
    10041004 Vals.push_back(VE.getValueID(II.getSuccessor(0)));
    10051005 if (II.isConditional()) {
    10061006 Vals.push_back(VE.getValueID(II.getSuccessor(1)));
    10141014 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
    10151015 Vals.push_back(VE.getValueID(I.getOperand(i)));
    10161016 break;
    1017 case Instruction::IndBr:
    1018 Code = bitc::FUNC_CODE_INST_INDBR;
    1019 Vals.push_back(VE.getTypeID(I.getOperand(0)->getType()));
    1020 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i)
    1021 Vals.push_back(VE.getValueID(I.getOperand(i)));
    1022 break;
    1023
    10171024 case Instruction::Invoke: {
    10181025 const InvokeInst *II = cast(&I);
    10191026 const Value *Callee(II->getCalledValue());
    21282128 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
    21292129 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
    21302130 }
    2131 }
    2132
    2133 void SelectionDAGLowering::visitIndBr(IndBrInst &I) {
    2134 fprintf(stderr, "indbr codegen not implemented yet");
    2135 abort();
    21312136 }
    21322137
    21332138
    4848 class GCFunctionInfo;
    4949 class ICmpInst;
    5050 class IntToPtrInst;
    51 class IndBrInst;
    5152 class InvokeInst;
    5253 class InsertElementInst;
    5354 class InsertValueInst;
    447448 void visitRet(ReturnInst &I);
    448449 void visitBr(BranchInst &I);
    449450 void visitSwitch(SwitchInst &I);
    451 void visitIndBr(IndBrInst &I);
    450452 void visitUnreachable(UnreachableInst &I) { /* noop */ }
    451453
    452454 // Helpers for visitSwitch
    18311831 writeOperand(BI.getSuccessor(1), true);
    18321832
    18331833 } else if (isa(I)) {
    1834 // Special case switch statement to get formatting nice and correct...
    1834 // Special case switch instruction to get formatting nice and correct.
    18351835 Out << ' ';
    18361836 writeOperand(Operand , true);
    18371837 Out << ", ";
    18451845 writeOperand(I.getOperand(op+1), true);
    18461846 }
    18471847 Out << "\n ]";
    1848 } else if (isa(I)) {
    1849 // Special case indbr instruction to get formatting nice and correct.
    1850 Out << ' ';
    1851 writeOperand(Operand, true);
    1852 Out << ", ";
    1853 Out << " [";
    1854
    1855 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
    1856 if (i != 1)
    1857 Out << ", ";
    1858 writeOperand(I.getOperand(i), true);
    1859 }
    1860 Out << ']';
    18481861 } else if (isa(I)) {
    18491862 Out << ' ';
    18501863 TypePrinter.print(I.getType(), Out);
    102102 case Ret: return "ret";
    103103 case Br: return "br";
    104104 case Switch: return "switch";
    105 case IndBr: return "indbr";
    105106 case Invoke: return "invoke";
    106107 case Unwind: return "unwind";
    107108 case Unreachable: return "unreachable";
    30853085 setSuccessor(idx, B);
    30863086 }
    30873087
    3088 //===----------------------------------------------------------------------===//
    3089 // SwitchInst Implementation
    3090 //===----------------------------------------------------------------------===//
    3091
    3092 void IndBrInst::init(Value *Address, unsigned NumDests) {
    3093 assert(Address);
    3094 ReservedSpace = 1+NumDests;
    3095 NumOperands = 1;
    3096 OperandList = allocHungoffUses(ReservedSpace);
    3097
    3098 OperandList[0] = Address;
    3099 }
    3100
    3101
    3102 /// resizeOperands - resize operands - This adjusts the length of the operands
    3103 /// list according to the following behavior:
    3104 /// 1. If NumOps == 0, grow the operand list in response to a push_back style
    3105 /// of operation. This grows the number of ops by 2 times.
    3106 /// 2. If NumOps > NumOperands, reserve space for NumOps operands.
    3107 /// 3. If NumOps == NumOperands, trim the reserved space.
    3108 ///
    3109 void IndBrInst::resizeOperands(unsigned NumOps) {
    3110 unsigned e = getNumOperands();
    3111 if (NumOps == 0) {
    3112 NumOps = e*2;
    3113 } else if (NumOps*2 > NumOperands) {
    3114 // No resize needed.
    3115 if (ReservedSpace >= NumOps) return;
    3116 } else if (NumOps == NumOperands) {
    3117 if (ReservedSpace == NumOps) return;
    3118 } else {
    3119 return;
    3120 }
    3121
    3122 ReservedSpace = NumOps;
    3123 Use *NewOps = allocHungoffUses(NumOps);
    3124 Use *OldOps = OperandList;
    3125 for (unsigned i = 0; i != e; ++i)
    3126 NewOps[i] = OldOps[i];
    3127 OperandList = NewOps;
    3128 if (OldOps) Use::zap(OldOps, OldOps + e, true);
    3129 }
    3130
    3131 IndBrInst::IndBrInst(Value *Address, unsigned NumCases,
    3132 Instruction *InsertBefore)
    3133 : TerminatorInst(Type::getVoidTy(Address->getContext()), Instruction::IndBr,
    3134 0, 0, InsertBefore) {
    3135 init(Address, NumCases);
    3136 }
    3137
    3138 IndBrInst::IndBrInst(Value *Address, unsigned NumCases, BasicBlock *InsertAtEnd)
    3139 : TerminatorInst(Type::getVoidTy(Address->getContext()), Instruction::IndBr,
    3140 0, 0, InsertAtEnd) {
    3141 init(Address, NumCases);
    3142 }
    3143
    3144 IndBrInst::IndBrInst(const IndBrInst &IBI)
    3145 : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndBr,
    3146 allocHungoffUses(IBI.getNumOperands()),
    3147 IBI.getNumOperands()) {
    3148 Use *OL = OperandList, *InOL = IBI.OperandList;
    3149 for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i)
    3150 OL[i] = InOL[i];
    3151 SubclassOptionalData = IBI.SubclassOptionalData;
    3152 }
    3153
    3154 IndBrInst::~IndBrInst() {
    3155 dropHungoffUses(OperandList);
    3156 }
    3157
    3158 /// addDestination - Add a destination.
    3159 ///
    3160 void IndBrInst::addDestination(BasicBlock *DestBB) {
    3161 unsigned OpNo = NumOperands;
    3162 if (OpNo+1 > ReservedSpace)
    3163 resizeOperands(0); // Get more space!
    3164 // Initialize some new operands.
    3165 assert(OpNo < ReservedSpace && "Growing didn't work!");
    3166 NumOperands = OpNo+1;
    3167 OperandList[OpNo] = DestBB;
    3168 }
    3169
    3170 /// removeDestination - This method removes the specified successor from the
    3171 /// indbr instruction.
    3172 void IndBrInst::removeDestination(unsigned idx) {
    3173 assert(idx < getNumOperands()-1 && "Successor index out of range!");
    3174
    3175 unsigned NumOps = getNumOperands();
    3176 Use *OL = OperandList;
    3177
    3178 // Replace this value with the last one.
    3179 OL[idx+1] = OL[NumOps-1];
    3180
    3181 // Nuke the last value.
    3182 OL[NumOps-1].set(0);
    3183 NumOperands = NumOps-1;
    3184 }
    3185
    3186 BasicBlock *IndBrInst::getSuccessorV(unsigned idx) const {
    3187 return getSuccessor(idx);
    3188 }
    3189 unsigned IndBrInst::getNumSuccessorsV() const {
    3190 return getNumSuccessors();
    3191 }
    3192 void IndBrInst::setSuccessorV(unsigned idx, BasicBlock *B) {
    3193 setSuccessor(idx, B);
    3194 }
    3195
    3196 //===----------------------------------------------------------------------===//
    3197 // clone() implementations
    3198 //===----------------------------------------------------------------------===//
    3199
    30883200 // Define these methods here so vtables don't get emitted into every translation
    30893201 // unit that uses these classes.
    30903202
    34093521 return New;
    34103522 }
    34113523
    3524 IndBrInst *IndBrInst::clone() const {
    3525 IndBrInst *New = new IndBrInst(*this);
    3526 New->SubclassOptionalData = SubclassOptionalData;
    3527 if (hasMetadata())
    3528 getContext().pImpl->TheMetadata.ValueIsCloned(this, New);
    3529 return New;
    3530 }
    3531
    3532
    34123533 InvokeInst *InvokeInst::clone() const {
    34133534 InvokeInst *New = new(getNumOperands()) InvokeInst(*this);
    34143535 New->SubclassOptionalData = SubclassOptionalData;
    2323 ret i32 16
    2424 }
    2525
    26
    27
    28 define i32 @indbrtest(i8* %P, i32* %Q) {
    29 indbr i8* %P, [label %BB1, label %BB2, label %BB3]
    30 BB1:
    31 indbr i32* %Q, []
    32 BB2:
    33 indbr i32* %Q, [label %BB1, label %BB2]
    34 BB3:
    35 ret i32 2
    36 }