對IR中 BitcastInst語句的處理算法

處理BitcastInst語句,首先總結其遇到的情況:
1、通過struct Value 遍歷到該變量user爲BitcastInst。

  %35 = bitcast (%1) to i16*
  //%1爲struct Value的user

(1)當%1爲struct全局或局部變量時

(1.1)當被memcpy語句使用時,通過memcpyToBitCast(memcpy_dest, bitcast_src, instr)函數處理
case:memcpy(i8* %2, i8* bitcast (%struct.str* @global to i8*), i64 28, i32 4, i1 false)
(1.2)當被store語句使用時,需要將store中的變量利用內存排布表拆開。
case:

%36 = bitcast %struct.MySize* %12 to i64*
store i64 %10, i64* %36, align 4

(1.3)當被load使用時,直接構建該變量的內存排布表。
case:

  %43 = bitcast %struct.point* %42 to i16*
  %44 = load i16, i16* %43, align 2

(2)當%1不爲全局或局部變量時
(2.1)爲GetElementPtrConstantExpr
case:

%1 = bitcast (%struct.point* getelementptr inbounds (%struct.str, %struct.str* @global, i64 0, i32 3) to i32*)

(2.2)爲GetElementPtrInst時,
case:

%52 = getelementptr inbounds [2 x %struct.point], [2 x %struct.point]* %global_0, i64 0, i64 1
%53 = bitcast %struct.point* %52 to i16*

構建內存排布表算法:
1、出現struct 的user爲’ bitcast struct to iN ’ instruction時,構建iN 的struct內存排布表。
2、遍歷bitcastInst 的userlist,移位操作指令創建Table。
3、再遍歷bitcastInst 的userlist,當其Table中只存有single variable時,指令替換。
4、替換完成,將所有Table delete。

當處理如下情況時:
structure value 作爲CallInst參數

  // 原始code
  // %64 = bitcast %struct.MyPoint* %23 to i64*
  // %65 = load i64, i64* %64, align 4
  // %66 = call i32 @cascadeClassifier([320 x i32]* %62, [320 x i32]* %63, i64 %65)
  //@cascadeClassifier(%10 is %65)
  // %36 = bitcast %struct.MySize* %12 to i64*
  // store i64 %10, i64* %36, align 4
  // 解決思路:首先將拆解完的struct (iN x, iN y) 傳遞給Function,作爲Functionde arguments, 如果Function使用的是x和y,直接替換爲x,y,否則使用BitConcatenate()函數合併x,y輸出並傳遞下去,直到找到使用x,y。
  //-->> %66 = call i32 @cascadeClassifier([320 x i32]* %62, [320 x i32]* %63, i32 %x, i32 %y)
  //-->> @cascadeClassifier([320 x i32]*, [320 x i32]*, i32, i32)
  // i64 %10 = BitConcatenate(i32 %x,i32 %y);
  // store i64 %10, i64* %36, align 4

  // %13 = getelementptr inbounds %struct.MySize, %struct.MySize* %12, i32 0, i32 0
  // %14 = getelementptr inbounds %struct.MySize, %struct.MySize* %12, i32 0, i32 1
  // store i32 %2, i32* %13
  // store i32 %3, i32* %14
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章