[llvm-commits] CVS: llvm-gcc/gcc/llvm-expand.c

Chris Lattner lattner at cs.uiuc.edu
Fri May 7 11:55:02 PDT 2004


Changes in directory llvm-gcc/gcc:

llvm-expand.c updated: 1.35 -> 1.36

---
Log message:

Implement constant folding of integer constant casts to bool.  This should speed
up the front-end a bit and expose further opportunities.

create_cond_branch is about to get spiffier, so bracer ourselves.

Remove some dead variables.



---
Diffs of the changes:  (+26 -7)

Index: llvm-gcc/gcc/llvm-expand.c
diff -u llvm-gcc/gcc/llvm-expand.c:1.35 llvm-gcc/gcc/llvm-expand.c:1.36
--- llvm-gcc/gcc/llvm-expand.c:1.35	Fri May  7 11:23:37 2004
+++ llvm-gcc/gcc/llvm-expand.c	Fri May  7 11:55:09 2004
@@ -193,11 +193,18 @@
 
   /* Constant fold some simple, but common, constants */
   if (V->VTy == Constant) {
-    if (V2C(V)->Repr[0] == '0' && V2C(V)->Repr[1] == 0) {
+    llvm_constant *C = V2C(V);
+    if (C->Repr[0] == '0' && C->Repr[1] == 0) {
       return llvm_constant_get_null(Ty);   /* At least handle the 0 case now */
     } else if (Ty == LongTy) {              /* Common array idx cases */
       if (V->Ty == UIntTy || V->Ty == IntTy)
-        return llvm_constant_new(Ty, V2C(V)->Repr);
+        return llvm_constant_new(Ty, C->Repr);
+    } else if (Ty == BoolTy) {
+      if (C->Repr[0] >= '0' && C->Repr[0] <= '9') {
+        assert(llvm_constant_get_integer_val(C) != 0 &&
+               "Null should have been handled already!");
+        return llvm_constant_bool_true;
+      }
     }
   }
 
@@ -3163,10 +3170,13 @@
   llvm_basicblock *TestBlock = llvm_basicblock_new("shortcirc_next");
   llvm_basicblock *DoneBlock = llvm_basicblock_new("shortcirc_done");
   llvm_instruction *PHI;
+  llvm_instruction *CondBr;
 
   FirstOp = cast_if_type_not_equal(Fn, FirstOp, BoolTy);
-  append_inst(Fn, create_cond_branch(FirstOp, isAndExpr ? TestBlock : DoneBlock,
-                                     isAndExpr ? DoneBlock : TestBlock));
+  CondBr = create_cond_branch(FirstOp, isAndExpr ? TestBlock : DoneBlock,
+                              isAndExpr ? DoneBlock : TestBlock);
+  append_inst(Fn, CondBr);
+              
   llvm_emit_label(Fn, TestBlock);
 
   SecondOp = llvm_expand_expr(Fn, TREE_OPERAND(exp, 1), 0);
@@ -3175,6 +3185,18 @@
   
   llvm_emit_label(Fn, DoneBlock);
 
+  /* If the conditional branch was folded into an unconditional branch (because
+   * the condition was known true or false, don't insert a PHI node, just use
+   * the known value instead.
+   */
+  if (CondBr->NumOperands == 1) {
+    llvm_type *Ty = llvm_type_get_from_tree(TREE_TYPE(exp));
+    if (CondBr->Operands[0] == D2V(FromBlock))
+      return cast_if_type_not_equal(Fn, isAndExpr ? llvm_constant_bool_false :
+                                    llvm_constant_bool_true, Ty);
+    return cast_if_type_not_equal(Fn, SecondOp, Ty);
+  }
+
   /* Add a PHI node to merge together the two computed values */
   PHI = llvm_instruction_new(BoolTy, "shortcirc_val", O_PHINode, 4);
   PHI->Operands[0] = isAndExpr ? llvm_constant_bool_false :
@@ -4077,9 +4099,6 @@
 
 static llvm_value *llvm_expand_minmaxabs_expr(llvm_function *Fn, tree exp,
                                               llvm_value *op0, llvm_value *op1){
-  llvm_basicblock *Stub = llvm_basicblock_new("minmaxabs");
-  llvm_basicblock *Cont = llvm_basicblock_new("cont");
-  llvm_basicblock *FromBlock =llvm_ilist_back(llvm_basicblock, Fn->BasicBlocks);
   enum InstOpcode Opcode;
   llvm_value *Compare;
   llvm_instruction *Select;





More information about the llvm-commits mailing list